0

I have a link with an inline onclick event:

<a href="#" onclick="somefunction();">click</a>

I registered an additional function on the onclick event:

jQuery("#addMoreOptions").live('click',function(){
        console.log('clicked');
    });

Which works fine when I click on the link on the browser, but not when I simulate programmatically:

jQuery("#addMoreOptions").click();

The programatical version triggers the inline event but not the "live" one.

When you have multiple functions attached to an event, what order does it use?

7
  • 1
    Works for me: jsfiddle.net/mattlunn/zG9Jw. What version of jQuery are you using/ can you produce a fiddle where this breaks/ what browser are you using? Commented Sep 8, 2011 at 12:13
  • not getting what you are saying...what do you mean by simulate programmatically ? Commented Sep 8, 2011 at 12:14
  • The fact that you're registering the event with .live is making me think that when you issue the .click() the element doesnt exist yet. Commented Sep 8, 2011 at 12:18
  • You're right your example works. I am using Chrome, latest version of jQuery. I will look more into and get back. Commented Sep 8, 2011 at 12:19
  • I agree with @Jamiec, is the element loaded dynamically? if not, just for fun. add a delay before you fire the click event. Commented Sep 8, 2011 at 12:24

2 Answers 2

1

I am pretty sure this is caused by the order of things happening.

If you look at this live example you'll see everything works as expected. This is because the event is registered, and then called. The code looks like:

jQuery(document).ready(function(){   

    jQuery("#addMoreOptions").live('click',function(){
        console.log('clicked');
    });

     $('#addMoreOptions').click();      

});

function somefunction()
{
 alert("clicked");   
}

When the page loads, you get an alert and a console.log.

Now with the very small change of putting the $('#addMoreOptions').click(); before registering the event as in this live example you only get the alert from the inline function.

For reference the code is

jQuery(document).ready(function(){   
     $('#addMoreOptions').click();  

    jQuery("#addMoreOptions").live('click',function(){
        console.log('clicked');
    });   

});

function somefunction()
{
 alert("clicked");   
}
Sign up to request clarification or add additional context in comments.

1 Comment

I removed the "inline" code and moved it to the live() call, so I have more control over the order of things. It fixed it.
0

An alternative way of triggering a click event would be to use the .trigger() function:

jQuery('#addMoreOptions').trigger('click');

2 Comments

Considering all click() does is call trigger('click') behind the scenes, this will make no difference.
There's no difference, the problem may lay in the live function not binding the event before it's called.

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.