3

Example html:

<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>

<div class="apple"></div>

I want to loop through the divs and filter them out if they don't have a class of either 'red', 'green', or 'blue'.

var onlyColorDivs = $('div').hasClass( __________ );

Is there a way to filling the blank in the previous line to accomplish this. Or am I going to have to put my list color classes in an array and do a loop?

Let me know if any clarification is needed.

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
Lokesh Dhakar
  • 5,289
  • 5
  • 22
  • 24

6 Answers6

8

All the answers are great and are appropriate. But, if you need a function like this a lot, you could also Monkey Patch the hasClass method to do what you want:

var _hasClass = $.fn.hasClass;
$.fn.hasClass = function (classNames) {
  if (!classNames || typeof classNames === "string" ) {
    return _hasClass.call(this, classNames); // Default behavior
  } else {
    // Take array and parse it for the filter method
    var classes = '.' + classNames.join(', .');
    return this.filter(classes).length > 0;
  }
};

Then you can use it like this:

$("div").hasClass(['red','green','blue']);
// or
$("div").hasClass('green');

Demo on JS Bin

Doug Neiner
  • 65,509
  • 13
  • 109
  • 118
  • I like the simplicity of this approach; sending multiple classes to hasClass. To avoid changing jquery to much, I opted to use filter() instead. Thanks for the code and the demo though. Like! – Lokesh Dhakar Aug 17 '10 at 22:52
4

Is there something wrong with using the conventional selection way?

$(".red, .green, .blue");

If you already have a collection you want to filter, say

$("div");

You can use the filter function as mentioned, but that would be slower, so if you have a choice, code it in a single selector.

Extending jQuery for an already supported functionality that's available with a different syntax is unnecessary. If you insist on using an array as input, it would be faster to join it in a selector:

var classes = new Array("red", "green", "blue");
$("div." + classes.join(",div."));
Boyan
  • 456
  • 3
  • 16
2

This will select only <div> elements that have at least one of those 3 classes.

Try it out: http://jsfiddle.net/gADQn/

var onlyColorDivs = $('div.red, div.green, div.blue');
user113716
  • 318,772
  • 63
  • 451
  • 440
2
.filter('.red,.green,.blue')

Should do the job.

Or just initially select them:

$('.red,.green,.blue', context)
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
0

.filter(selector) is the function you are looking for. Calling this on a selection of jQuery elements will refine the selection to those elements that match the selector you provide. Additionally, you could simply only select those elements to begin with.

So either: $('div').filter('.red, .green, .blue');

Or: $('div.red, div.green, div.blue');

Here's the documentation on using the filter function: http://api.jquery.com/filter/

And finally here's a demonstration of using it: http://jsfiddle.net/Etb7R/

This little script selects all <div>s and fills them with the text of their class attribute, then filters down to only those with the classes 'red', 'green', or 'blue' and applies an additional class to highlight them. Hopefully that should give you a good idea of your range of options.

Ender
  • 14,995
  • 8
  • 36
  • 51
-1

Determine if an element has a CSS class with jQuery

http://docs.jquery.com/Traversing/hasClass

Community
  • 1
  • 1
XstreamINsanity
  • 4,176
  • 10
  • 46
  • 59
  • I'm looking to check multiple classes in one statement. The question you linked too has examples for checking for a single class. – Lokesh Dhakar Aug 17 '10 at 16:39
  • Yeah, I commented before fully reading the question. Because your reputation was around as low as mine, I thought maybe you were asking if there was a hasclass function. I'm new to jquery so I wasn't sure. Sorry about that. – XstreamINsanity Aug 17 '10 at 16:46