Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop keypress event

How to stop keypress event in keydown.

I have a keydown handler in that I need to stop keypress event.

Actually I have a form and textbox in it.

When user press enter key, keydown is triggering the event and having the handler.

Form submit will triggered be in keypress, so I need to stop the keypress event in my keydown handler.

like image 399
Santhosh Avatar asked Sep 10 '09 10:09

Santhosh


People also ask

How do I turn off keypress events?

Alternatively, you could attach another event handler to the input field, and in this handler stop the propagation of the event: jQuery('#input-field-id'). bind('keypress', function(e) { e. stopPropagation(); });

How to remove keydown event in JavaScript?

querySelector(". element"). removeEventListener("keydown"); });

What is the difference between Keydown and keypress events?

The keydown event is fired when a key is pressed. Unlike the keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.


1 Answers

function onKeyDown(event) {      event.preventDefault(); } 

http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event-preventDefault

or

function onsubmit(event) {   return false; } 

return false to stop events propagation

like image 133
john Avatar answered Oct 03 '22 23:10

john