Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a wildcard selector for identifiers (id)?

If I have an unknown amount of identifiers sharing a specific naming-scheme, is there a way to grab them all at once using jQuery?

// These are the IDs I'd like to select #instance1 #instance2 #instance3 #instance4  // What do I need to add or how do I need to modify this jQuery selector in order to select all the IDs above? ("#instanceWILDCARD").click(function(){} 
like image 604
Matt Elhotiby Avatar asked Sep 13 '10 02:09

Matt Elhotiby


People also ask

What is a wildcard selector?

Suggest Edits. Wildcards are symbols that enable you to replace zero or multiple characters in a string. These can be quite useful when dealing with dynamically-changing attributes in a selector.

Can you use wildcard in CSS selector?

Wildcard selectors allow you to select multiple matching elements. In CSS, three wildcard selectors exist i.e. $, ^, and * The * wildcard is known as the containing wildcard since it selects elements containing the set value. With the ^ wildcard, you get to select elements whose attribute value starts with the set ...

What is wildcard in HTML?

A wildcard character is used to substitute one or more characters in a string. Wildcard characters are used with the LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.


1 Answers

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 }); 
like image 109
Nick Craver Avatar answered Sep 30 '22 01:09

Nick Craver