78

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(){}
1
  • 8
    All your examples are of identifiers, not classes.
    – Kirk Woll
    Commented Sep 13, 2010 at 2:23

9 Answers 9

193

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
});
6
  • 6
    Funny but not answering the question (Eric does) Commented Jan 29, 2014 at 8:01
  • 2
    @RuneJeppesen it does if you look at the actual code in the question, the OP is confused with class vs id terminology which is a problem I agree. Commented Jan 29, 2014 at 12:24
  • 2
    @NickCraver I hate to be that guy, but it doesn't. You do, however, solve his problem. I had the same question and your answer does not solve my problem (The elements in question is from a third party plugin) Commented Jan 29, 2014 at 19:17
  • 1
    @RuneJeppesen It really does, look at the question, it's a list of IDs he wanted to match against. I show how to do that as well as a better suggestion of how to do it - if you have control to do so. Commented Jan 29, 2014 at 21:46
  • That is not the question - that is the code. The question in the headline is 'Is there a wildcard class selector' (you give an ID wildcard) You kind of answer 'If i have a few classes named something similar is there a way to grab them all in one shot' but your answer is to add a new classname.. Commented Jan 29, 2014 at 22:32
94

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

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!

1
  • +1, nice! I tried this for two UL elements (nested menu) using $('ul:likeClass(menu_)').addClass('found_menu'); which works fine for testing. Do you have an idea how to extract the 'wildcarded' part of the class name, like 'left' or 'right', in case the class names are 'menu_left' and 'menu_right' ?
    – ddlab
    Commented Jun 8, 2017 at 13:28
11

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

4

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.

3

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

1
  • Because I'm not a creator of that webpage.
    – BingLi224
    Commented Oct 24, 2022 at 9:44
2

Use the carrot.

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

jsFiddle example

0
-1

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.

-2

We can do it this way:

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

    });
});
1
  • 1
    live is deprecated. Use on.
    – nhahtdh
    Commented Sep 5, 2013 at 16:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.