0

I'd like to invoke default keydown event handler from javascript. Is it possible?

3
  • 1
    what is the default keydown event handler? On what element? Why? Commented Dec 21, 2009 at 11:10
  • 1
    Not 100% the same but a related SO post here: stackoverflow.com/questions/202285/… Commented Dec 21, 2009 at 11:11
  • >> Not 100% the same but a related SO post here: stackoverflow.com/questions/202285/ o.k.w, thank. Commented Dec 21, 2009 at 11:16

2 Answers 2

3

If the event has an explicit event handler you can just invoke it directly:

// Precondition - the element has an explicit handler registered
element.onkeydown();

Otherwise, there's no way to explicitly tell the browser to do "what it would have done anyway". The only way to get this to happen is to not stop the event from bubbling - which can be a real pain if you want to set a timeout and then allow the event to continue, it's essentially not possible.

In most cases, though, you can invoke your own code on an event handler and let the keyDown event continue to the browser. And if this isn't possibel for whatever reason, you can usually write your own method that will simulate the effects of the event (e.g. change the content of an input field, submit the form, etc.)

Sign up to request clarification or add additional context in comments.

Comments

0

There is no default keydown event. The keydown event occurs when the key is pressed on any form elements, followed immediately by the keypress event, and possibly the textInput event on text box when you enter a value. Then the keyup event is generated when the key is released The following example shows the use of the onKeyDown event handler to display a message in the text box.

<body><form action="" method="POST" id="myForm"><input type="text" name="myText" onKeyDown="changeVal()"><script type="text/javascript" language="JavaScript">s1 = new String(myForm.myText.value)function changeVal() {    s1 = "You pressed a key"
myForm.myText.value = s1.toUpperCase() }</script></form></body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.