I have been tasked with creating a directive that allows users to navigate to the next input or button in a modal window:
.directive('autoVrm', function () {
return function (scope, element, attrs) {
var counter = 0;
element.bind("keydown keypress", function (event) {
console.log(element);
if(event.which === 13) {
counter++;
event.preventDefault();
var elementToFocus = element.find('input')[counter] || element.find('button')[1];
console.log(elementToFocus.type);
if(angular.isDefined(elementToFocus))
elementToFocus.focus();
if (elementToFocus.type === 'button'){
counter = -1;
elementToFocus.bind("keydown keypress", function (eventSubmit) {
if(eventSubmit.which === 13) {
console.log('submit');
}
});
}
}
});
};
However, I am encountering an issue where clicking enter while on a button results in:
Uncaught TypeError: undefined is not a function
Why does this happen? How can I enable the enter key to submit the form without needing to involve the controller?