tags:

views:

68

answers:

3

Can I do something like the below where I can have multiple classes trigger an event?

$('a.red a.blue a.green').click(function(event) 
{

});
+11  A: 

Yes, you can use the comma as separator.

$('a.red, a.blue, a.green').click(function(event) {});
BalusC
A: 

$('a.red.blue.green') selects the elements that have all those classes.

$('a.red, a.blue, a.green') selects the elements that have atleast one of those classes.

Hailwood
For all those classes, it is $("a.red.blue.green").
Jimmie Lin
+3  A: 

A slightly different approach would be to prefix your classes like a.prefix_red, a.prefix_blue, a.prefix_green and then use a wildcard selector on it like:

$("a[class^=prefix_]")

The advantage is that as long as you prefix all your "trigger" classes, you don't have to edit the jQuery every time you add a new one, not that it would be a major edit anyways, but might come in handy if you decide to minify your script for example.

FreekOne