Anthony Ryan
Anthony Ryan

Reputation: 395

Prevent enter key from triggering button

I have a search input box which when a user presses enter needs to do nothing. I am using EmberJS and Jquery with the below code. Currently it works to disable a pop up from being triggered but for some reason in IE9 when enter is pressed a toggle button becomes in focus. Works fine in Chrome. I've tried tabindex and preventDefault but neither do the trick.

   if ($el.hasClass('form-control')) {
                    if ( e.which == 13) {
                        this.get('controller').flipit();

                    }
                }

Thank you.

EDIT------

Here is a snippet of my page. When a user hit enter within the search box the button to the right is getting highlighted. How would I prevent this?

enter image description here

Upvotes: 4

Views: 3417

Answers (2)

Anthony Ryan
Anthony Ryan

Reputation: 395

The solution:

   if ($el.hasClass('form-control')) {
                    if (e.key == 13) {
                        this.get('controller').flipit();
                    }

                    e.stopPropagation();
                    e.preventDefault();
                    return false;

                }

Upvotes: 0

Rafael
Rafael

Reputation: 7746

Do this

$("#inputBox").on('keydown', function(event) {
    if (event.key == "Enter") event.preventDefault();
});

Upvotes: 3

Related Questions