0

I've faced multiple times when I try to use .click() function that triggers the click event multiple times and have solved anyway previously and didn't noticed about this. And this time also I was facing the same problem and after an hour I noticed why this is happening. Below is an example:

wrong: (triggering click events multiple times)

$(selector).on('click',function(){
//do some func
}
$(another-selector).on('click',function(){
$(selector).click(); //trigger previous selector click function
}

right: (triggers once as expected)

$(another-selector).on('click',function(){
$(selector).click(); //trigger function before calling the function
}
$(selector).on('click',function(){
//do some func
}
3
  • What are selector and another-selector? Maybe another-selector is subset of selector? Commented Dec 20, 2013 at 8:49
  • And what are you actually asking? Commented Dec 20, 2013 at 8:50
  • @idlerboris yeah the another-selector is outside the selector markup Commented Dec 20, 2013 at 8:52

3 Answers 3

1

You are missing ");" in some places

Here is how you should write it

$(selector).on('click',function(){
    //do some func
});
$(another-selector).on('click',function(){
    $(selector).click(); //trigger previous selector click function
});
Sign up to request clarification or add additional context in comments.

1 Comment

@C-link Actually it is... Check this fiddle jsfiddle.net/BaXJ7 The code is correct, but if you remove the two ); I added, it wont work. You are not closing your .on functions.
0
$(selector).on('click',function(){
//do some func
}
$(another-selector).on('click',function(){
$(selector).trigger('click'); //trigger previous selector click function
}

Comments

0

Are you aware of https://www.google.nl/search?q=event+propagation?

The issue could be anything, need some HTML and real selectors to answer this one...

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.