Button Click event fires when pressing Enter key in different input (no forms) Button Click event fires when pressing Enter key in different input (no forms) jquery jquery

Button Click event fires when pressing Enter key in different input (no forms)


I had the same problem and solved it by adding type="button" attribute to the <button> element, by which IE thinks the button as a simple button instead of a submit button (which is default behavior of a <button> element).


I ran into this problem today. IE assumes that if you press the enter key in any of the text fields, you want to submit the form -- even if the fields are not part of a form and even if the button is not of type "submit".

You must override IE's default behavior with preventDefault(). In your jQuery selector, put in the div that contains the text boxes you want to ignore the enter key -- in your case, the "page" div. Instead of selecting the whole div, you could also specify the text boxes you want to ignore specifically.

$('#page').keypress(function(e) {    if(e.which == 13) { // Checks for the enter key        e.preventDefault(); // Stops IE from triggering the button to be clicked    }});


I was having this issue with ASP.NET WebForms:

<asp:Button />

This can NOT be solved by just adding type="button" because ASP.NET replaces it with type="submit" (you can see this behavior in the browser if you inspect the element.)

The correct way to do this in asp.net is to add

<asp:Button UseSubmitBehavior="false" />

to the button. ASP will automatically add the type="button" attribute.