Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85643

Why click function is triggering multiple times?

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
}

Upvotes: 0

Views: 96

Answers (3)

Armen Abrami
Armen Abrami

Reputation: 224

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
});

Upvotes: 1

Erik Nijland
Erik Nijland

Reputation: 1199

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

Upvotes: 0

Arthur Halma
Arthur Halma

Reputation: 4001

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

Upvotes: 0

Related Questions