tags:

views:

76

answers:

4

If i have a few classes named something similar is there a way to grab them all in one shot

#instance1
#instance2
#instance3
#instance4

("#instance").click(function()
//GRAB ALL OF THEM
A: 

Why don't you just assign class = "instance" to all of them and select them using $('.instance')?

casablanca
+2  A: 

Those are IDs, but you can do something similar to:

$("[id^='instance']").click(...)

That's a bit expensive though - it helps if you can specify either a) the type of element or b) a general position in the DOM, such as:

$("#someContentDiv span[id^='instance']").click(...)

The [id^='...'] selector basically means "find an element whose ID starts with this string, similar to id$= (ID ends with this string), etc.

You can find a comprehensive list on the jQuery Docs page here.

mway
+5  A: 

The attribute starts-with selector ('^=) will work for your IDs, like this:

$("[id^=instance]").click(function() {
  //do stuff
});

However, consider giving your elements a common class, for instance (I crack myself up) .instance, and use that selector:

$(".instance").click(function() {
  //do stuff
});
Nick Craver
+1  A: 

Use the carrot.

$("div[id^=instance]").hide();

jsFiddle example

jhanifen
User the carrot? :)
Nick Craver
isn't this funny? :)
Reigel
It's a "caret". -_-;
Mike DeSimone
but carrot is so much more fun...
jhanifen