tags:

views:

58

answers:

3

Hi, 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!

+2  A: 

Yes you can use a comma-separator for binding events to multiple elements with different classnames/id's/etcetera.

If it isn't working, you have class "akslabel", might want to change that to "asklabel"...?

Justus Romijn
Thanks, actually it's in Norwegian so the word "accept" is "aksept" hence, "akslabel" the label pertaining to my "accept check box" :)
Kyle Sevenoaks
Aha so that makes sense :) ... and I love Royksopp!
Justus Romijn
Yeah, they're GREAT!
Kyle Sevenoaks
A: 

It's ok, it's even better then splitting. Why duplicate code?

Onkelborg
+5  A: 

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.

Nick Craver
I like waffles. Thanks!
Kyle Sevenoaks
Waffles with bacon?
Joeri Hendrickx
@Joeri - ....what else would you have with waffles?
Nick Craver
@Nick nothing... they just go by themselves. Maybe some whipped cream or powdered sugar (http://www.suikerfee.be/wafels/brusselse/).
Joeri Hendrickx