Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  185] [ 2]  / answers: 1 / hits: 27453  / 12 Years ago, tue, january 22, 2013, 12:00:00

How to call a Javascript event from another Javascript function by obtaining this event via getter?


Some example code is below:


<script type="text/javascript">
function keyDownHandler(elem, e)
{
var keyId = e.keyCode;
if(keyId === 13)
{
var clickMethod = elem.onclick;
//now call the clickHandler method.
}
}
</script>


<input type="text" onkeydown="keyDownHandler(this, event)"
onclick="clickHandler(this)" />

I want to invoke the method defined in onclick attribute of the input field.


More From » dom-events

 Answers
21

As Tim S. said, you can directly call handler, however if you really want to call the event, you can use Jquery 'trigger'.



$(elem).trigger('click')


if elem is not jquery object.



Edit: jquery is not required. The code should work by just adding the parenthesis in onclick call.



function keyDownHandler(elem, e) {
var keyId = e.keyCode;
if (keyId === 13) {
elem.onclick();
}
}


clickHandler must be defined.


[#80713] Sunday, January 20, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eden

Total Points: 730
Total Questions: 117
Total Answers: 117

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
;