julio
julio

Reputation: 6728

getting multiple class selectors for select in jQuery

I'm trying to get all select elements in a given div, even though they have different class names. It looks like this:

<div id="mydiv">
    <select class="myclass">...</select>
    <select class="myotherclass">...</select>
</div>

I'd like to get all the selects with myclass or myotherclass. I've tried several ways but can't get it right.

I've tried variations of the following:

$.each($('[#mydiv select.myclass][#mydiv select.myotherclass]'), function(){ // handle stuff
$.each($('#mydiv select.myclass select.myotherclass'), function(){ // handle stuff
$.each($('#mydiv select.myclass myotherclass'), function(){ // handle stuff

and others, but I can't seem to get both selects into my element selection.

Upvotes: 1

Views: 117

Answers (4)

Codegiant
Codegiant

Reputation: 2150

Try this

JS CODE

$('#mydiv select').each(function(){
 console.log($(this));
});

DEMO

Upvotes: 1

kilkadg
kilkadg

Reputation: 2317

I think it works with

$('#mydiv select.myclass, #mydiv select.myotherclass')

as in Css Selectors

Upvotes: 1

ButterDog
ButterDog

Reputation: 5225

$.each($("#mydiv .myclass, #mydiv .myotherclass"), function(){ // handle stuff });

Upvotes: 1

David Morton
David Morton

Reputation: 16505

Perhaps

$('#mydiv select.myclass, #mydiv select.myotherclass')

Upvotes: 7

Related Questions