Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I "stack up" selectors using jQuery?

Tags:

html

jquery

css

I know "selectors" is the wrong term, but I can't remember it :P

I have this script:

$(function() {
   //checkbox
   $("#checkbox2, .akslabel").click(function(){
    $("#scrollwrap").toggleClass('highlight');
   });
});

Is it ok to stack up the selectors like that? Or should I re-write it for each one?

Thanks!

like image 304
Kyle Avatar asked Mar 24 '26 07:03

Kyle


1 Answers

You can use as many selectors as you want combined with a , (the multiple selector) and bind the same anonymous function to them all, which is what your example does.

There's nothing wrong with this, it's cleaner/more concise code.

So can you "stack" selectors? Yes, think of them as waffles, very delicious waffles, with bacon.

like image 86
Nick Craver Avatar answered Mar 25 '26 22:03

Nick Craver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!