2

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.

2
  • see below example for solution Commented Jan 22, 2013 at 12:00
  • Did you try invoking it? What happened? Commented Jan 22, 2013 at 12:00

5 Answers 5

4

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.

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

6 Comments

OP does not seem to use jQuery, so he would have to included it, and elm would have to be a jQuery object.
no, its the element which is passed in the function 'elem' or you can get it using $("#elementId") or document.getElementById("elementId")
Of course... but it still has to be a jQuery object, not the DOM element. In the OP's code it's a DOM element.
That moment when one does not knows the difference between a DOM and an jQuery object.
yes, thats correct, if he can use Jquery its just the matter of wrapping it in $ e.g. $(elem).trigger('click')
|
1

I wouldn't recommend that.

You need to abstract the shared login out of the onclick handler as a separate method and then call the method from both the event handlers.

function sharedloginc(some arguments...) {
    //do something
}

function keyDownHandler(el, e) {
    //do something
    sharedloginc(....)   
}

function clickHandler(el, e){
    sharedloginc(....)
}

Comments

0

Why not use

<script type="text/javascript">
function keyDownHandler(elem, e)
{
    var keyId = e.keyCode;

    if(keyId === 13)
    {
        // call function, normally
        clickHandler(elem, null);
    }
}
</script>

I have set the event argument null, because, well, you can't generate a (correct!) event object.


Okay after some playing around I have managed to make this:

<script type="text/javascript">
function keydown_handler(el, e) {
    var click, code = e.keyCode;

    if(code === 13) {
        var handler = el.onclick;

        if(typeof handler === 'function') {
            handler.apply(this, el, null);
        }
    }
}

function click_handler(el, e) {
    alert('clicked!');
}
</script>

<input type="text" onkeydown="keydown_handler(this, event);" onclick="click_handler(this, event);" />

It seems to work, but I can't verify it works in all browsers at this point.

1 Comment

well thanks but i want the original function to be called. That one defined in the onlick attribute
0

This example will solve your problem.

<html>
<head>
<title>Testing</title>
</head>
<body>
             <script type="text/javascript">
                 function keyDownHandler(elem, e)
                   {
                       var keyId = e.keyCode;
                       if(keyId == 13)
                       {
                         // var clickMethod = elem.onclick;
                          clickHandler(elem, e);
                        }
                   }
                  function clickHandler(elem, e)
                  {
                    alert("clickHandler");
                  }
           </script>
       <input type="text" onkeydown="keyDownHandler(this, event)" onclick="clickHandler(this, event)" />
</body>
</html>

1 Comment

in which browser you are running your page
0

If you are looking for a one-liner within keydown/keyup event attributes. These are the possible options.

for executing the onclick/click event on the same element.

((e)=>{if(e.keycode===13) click():})(event)

or

((e)=>{if(e.keycode===13) this.click()})(event)

for executing the onclick/click event by elementID.

((e)=>{if(e.keycode===13) elementID.click()})(event)

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.