46

I have got some classes

.text-1
.text-2
.text-3

I want to select them all, how to do that? Thanks for the help

user469652
  • 48,855
  • 59
  • 128
  • 165

6 Answers6

70

Try this. For more details refer jquery selectors

$('*[class^="text"]')
Chinmayee G
  • 7,947
  • 2
  • 31
  • 41
  • 7
    The class attribute takes a space separated list of classes; this will fail on `class="foo text-1 bar"` – Quentin Nov 12 '10 at 05:35
  • 9
    Then we can use $('*[class*="text"]'). this will search text as substring in class attribute. Ref: http://api.jquery.com/attribute-contains-selector/ – Chinmayee G Nov 12 '10 at 05:37
  • 6
    @Chinmayee That will erroneously match something like `sometext` – Phil Nov 12 '10 at 05:39
  • @chinmayee , is he expecting all classes in that pattern or he just wants to know how to bind multiple classes together to a event. – kobe Nov 12 '10 at 05:41
  • sorry , his question was like that ...my mistake – kobe Nov 12 '10 at 05:43
  • Looks like jQuery could do with a regex attribute selector :) `$('[class~="text-\d+"]')` (yes, I know the `~=` attribute selector is already taken but it seemed like the most logical choice) – Phil Nov 12 '10 at 05:44
  • 1
    I guess then he has to use combination of both. $('*[class^="post"],*[class~="post"]') – Chinmayee G Nov 12 '10 at 05:44
  • @Phil it seems that solution you have provided is not working. Which version of jquery it requires? Check and correct if required http://jsfiddle.net/pSXVr/ – Chinmayee G Nov 12 '10 at 05:49
  • 2
    @Chinmayee Sorry, it's a fictional example. I was saying it would be nice if there were a regex attribute selector. The `~=` selector is the "contains word attribute selector" (http://api.jquery.com/attribute-contains-word-selector/) however it won't work here either – Phil Nov 12 '10 at 05:51
  • @Phil my mistake. I thought this is a feature that jquery has and I'm not aware of it. Thanks anyways. – Chinmayee G Nov 12 '10 at 05:52
14

Here's an attempt at a solution that's both accurate and not too slow:

var elts = $('*[class*="text-"]')
  .filter(function () {
    return this.className.match(/(?:^|\s)text-/);
  });

Which works by using the (hopefully) fast Sizzle code to find elements that have "text-" anywhere in their class attribute, and then calls a function on each of those to filter them down to the ones that actually have "text-" at the beginning of a class name.

hobbs
  • 223,387
  • 19
  • 210
  • 288
4

You don't necessarily need to specify asterisk *, you can do this too:

$('[class^="text-"]')

Notice the addition of - after text something you are looking for.

Check out the jQuery starts with selector for more information.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
4

You can do this with just two selectors in one function call:

$('[class^="text-"], [class*=" text-"]')

This will check if the class attribute starts with text- or if there is any part of the class string that has a space followed by text-. This solves any issues you might have with the

$('[class^="text-"], [class*=" text-"]')
    .css('background-color', '#00FF00'); // add background color for demo
/* just for demo */
div:after {
  content: "class: " attr(class);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="text-1 test"></div>
<div class="text some-text-1"></div>
<div class="nope text-long-class-name"></div>
<div class="some-text-1"></div>
<div class="even get text-n-here"></div>

You could pull this out into a function that selects all elements with a class that matches a given prefix, as well:

function selectElementsWithClassPrefix(prefix) {
    return $('[class^="' + prefix + '"], [class*=" ' + prefix + '"]');
}
Daniel Thompson
  • 535
  • 2
  • 9
0

Here's a function that deals with the issue of multiple classes, in cases like <div class="foo bar barfood">:

function classStartsWith(str) {
  return $('div').map( function(i,e) {
    var classes = e.className.split(' ');
    for (var i=0, j=classes.length; i < j; i++) {
      if (classes[i].substr(0, str.length) == str) return e;
    }
  }).get();
}

The function returns an array of matching DOM elements. You can use it thus:

$( classStartsWith('text-') ).doSomething();

Here's an example: http://www.jsfiddle.net/Ms6ey/3/

Of course this assumes the selector 'div'. The question seems to be asking about any element at all. It's easy enough to make the function take a selector as another argument, and use that in place of the hardcoded one in the example.

Ken Redler
  • 23,863
  • 8
  • 57
  • 69
-5
$('#qvDetails,#qvAdd,.qvFramedActive,#qvMoreSizes').live('mouseover',function(){

        $(this).addClass('qvHover');                                                                            

    });

example from one of my projects.you can mention both divs and classes together like above

you can provide as coma separated class or divs in the same...

kobe
  • 15,671
  • 15
  • 64
  • 91
  • I gave the above example if you want to give multple classes or ids together when there is no pattern – kobe Nov 12 '10 at 05:32
  • whats wrong with above code , he didn't mention that he wants a pattern right – kobe Nov 12 '10 at 05:39