-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
  • 4,492
  • 12
  • 50
  • 109

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
  • 48,585
  • 17
  • 95
  • 104
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
  • 51
  • 3