Reputation: 123
Is it possible to disable the enter keypress on an element in the dropdown in Twitter Typeahead? I'm using Angular with Typescript.
I've tried to use preventDefault()
when event.keycode === 13
on ng-keydown
in the typeahead input. ng-keydown
will detect enter has been pressed however there's no way to stop typeahead from selecting the element.
Do I need to disable a typeahead listener on enter or something?
Upvotes: 2
Views: 1136
Reputation: 211
Have you tried it on input
element or form
element?
For me it works when I execute:
$('form').on('submit', function(e) {event.preventDefault();});
However I suggest using more specific selector ;)
Or you can do it on input
field but you need to prevent the event from propagation. The preventDefault
only prevents the default behaviour for event - in case of input it's updating it's value. What you want to do is stop the form from submission so you need to stop the event propagation.
Here is the snippet that should work for input:
$('#demo-input').on('keydown', function(e) {
if (e.keyCode === 13) {
return false
}
});
When using jQuery the return false
in event handler does the same as calling e.preventDefault() and e.stopPropagation().
Upvotes: 2