-2

I am trying to select elements that have certain classes.

following will only select classA:

 $('.holder').children('div[class*=classA]');

Divs can either have classA or classB and other classes as well together!

so it can be like this:

<div class="holder">
    <div class="classA"/>
    <div class="classA otherClass"/>
    <div class="classB"/>
    <div class="classB thirdClass"/>
    <div class="whatever"/>
</div>

How do I select all elements with classA and classB?

Toniq
  • 3,918
  • 9
  • 42
  • 96

2 Answers2

2

You want to use , (comma) to separate your selectors:

$('#holder').children('.classA, .classB, .thirdClass, .otherClass')

Demo:

http://jsfiddle.net/mEAFh/1

Jared Farrish
  • 47,157
  • 17
  • 93
  • 101
1

You can use the "find" function, on the $('#holder') object as

var myDivs = $('#holder').find('.classA, .classB')

Loop the object myDivs to get each individual div with the requested class.

kostisalex
  • 41
  • 3