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.
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.