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(){}
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.
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 ...
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.
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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With