Is there a wildcard selector for identifiers (id)?

JqueryJquery SelectorsWildcard

Jquery Problem Overview


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(){}

Jquery Solutions


Solution 1 - Jquery

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
});

Solution 2 - Jquery

If you really want to match classes, not ids, the syntax is a bit more involved, since class can have multiple values.

// handle elements like <div class="someclass1"></div>
$('[class^="someclass"]').click(function() {
   // do stuff
});

// handle elements like <div class="foo someclass1"></div>
$('[class*=" someclass"]').click(function() {
   // do stuff
});

Solution 3 - Jquery

I'm surprised no one has mentioned creating your own filter selector (by extending jQuery's Selector functionality). Here I've created a wildcard selectors I called "likeClass" and "likeId" that accepts any wildcard string and will find all elements that are a match (similar to Regex matching).

Code:

$.expr[':'].likeClass = function(match){
      return $('[class*=" '+ match +'"]');
};
$.expr[':'].likeId = function(match){
      return $('[id*=" '+ match +'"]');
};

Example Usage:

Now let's say you had multiple div elements with similar names like .content-1, .content-2, .content-n... etc and you want to select them. Now it's cake! > $('div:likeClass(content-)'); // Returns all elements that have a similar Classname: content-*

or

> $('div:likeClass(content-)'); // Returns all elements that have a similar ID: content-*

Oh yeah, one more thing... you can chain it too. :)

$('li:likeId(slider-content-)').hide().addClass('sliderBlock').first().fadeIn('fast');

Enjoy!

Solution 4 - Jquery

No need for additional expr or anything fancy if you have jQuery

jQuery('[class*="someclass"]').click(function(){
});

jQuery('[id*="someclass"]').click(function(){
});

As noted: https://stackoverflow.com/a/2220874/2845401

Solution 5 - Jquery

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.

Solution 6 - Jquery

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

Solution 7 - Jquery

Use the carrot.

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

jsFiddle example

Solution 8 - Jquery

This is the only correct answer to the id wildcard question.

$('[id*="Wild card in double quotes"]') 

This will get "Wild card in double quotes. And there's more!!!", and also "More BS in front of Wild. Wild card in double quotes".

You should use quote marks that are different than your '[]' tags.

Solution 9 - Jquery

We can do it this way:

$(document).ready(function () {
    $('[id*=btnOk]').live("click", function () {

    });
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt ElhotibyView Question on Stackoverflow
Solution 1 - JqueryNick CraverView Answer on Stackoverflow
Solution 2 - JqueryEricView Answer on Stackoverflow
Solution 3 - JqueryTimothy PerezView Answer on Stackoverflow
Solution 4 - JqueryShinraiView Answer on Stackoverflow
Solution 5 - JquerymwayView Answer on Stackoverflow
Solution 6 - JquerycasablancaView Answer on Stackoverflow
Solution 7 - JqueryjhanifenView Answer on Stackoverflow
Solution 8 - JqueryKurtView Answer on Stackoverflow
Solution 9 - JqueryThomasView Answer on Stackoverflow