How do I run the same click function for 2 classes ?
I have this :
$('.buythis').click(function(){ blabla }
And I want to use the same line for a different class like :
$('.anotherclass').click(function(){ blabla }
Now ... how can I use the same click function without redeclaring it ? i want something like :
($('.buythis'),$('.anotherclass')).click(function(){ blabla }
Try this:
$('.buythis, .anotherclass').click(function(){ blabla }
Use a comma between the two selectors:
$('.buythis, .anotherclass').click( ... );
or use two selectors chained with .add()
.
$('.buythis').add('.anotherclass').click( ... );
The latter syntax can be useful if the selectors are complicated since it can remove ambiguity from the selector parser and make the code easier to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With