Esc key acts like reset button in html

I have two text fields with reset and submit buttons. The reset button works fine. But when I enter something into these two text fields and press esc, the values ​​disappear. The event acts as a reset button. I am not sure how to control this. I really appreciate your help ... Thank you ...

<input type="text" name="" /> <input type="text" name="" /> <input type="button" value="Search" /> <input type="reset" value="Reset" /> 
+2
source share
2 answers

It works fine in all browsers http://jsfiddle.net/xgTxK/2/

 $(document).keydown(function (e) { if(e.keyCode==27){ e.preventDefault(); } }); 

add above script to your code to prevent default functionality

+2
source

I'm not sure if this is compatible in all browsers, but I noticed that the esc button will usually reset the text entered in the text input, but only when focusing inside the text input. Or else, esc will reset the text if the onchange event has not yet occurred.

And I would suggest that you don’t need to use JavaScript to capture key events inside input and prevent default behavior.

+1
source

All Articles