user1017882
user1017882

Reputation:

Searching, recursively, the children of all divs of a certain class

<div class="testClass">
    <div>
        <select name="specialName123">
        </select>
    </div>
</div>

<div class="testClass">
    <div>
        <span id="someSpan">
            <select name="specialName123">
            </select>
        </span>
    </div>
</div>

How do I select the two above divs on my page based on the fact that they have the class "testClass" and have a select that has "123" in it's name?

I understand I can find all selects like so $('select[name*="123"]'), and I can use $(".testClass") to select the appropriate divs. But how do I put these together in the way I need to?

Upvotes: 3

Views: 1067

Answers (3)

Ram
Ram

Reputation: 144689

You can use closest method:

$('select[name*="123"]').closest('.testClass')

DEMO

or:

$('.testClass').has("select[name*=123]")

DEMO

Upvotes: 2

Zachary Kniebel
Zachary Kniebel

Reputation: 4774

You can also use the find method to get all the descendants of testClass, which you can then filter by a selector:

$('.testClass').find('select[name*="123"]');

Upvotes: 0

OptimusCrime
OptimusCrime

Reputation: 14863

Why not:

$('.testClass select[name*="123"]')

It maches any children of testclass (any depth) that is a select-box with name containing 123.

Upvotes: 1

Related Questions