T1000
T1000

Reputation: 2941

Select div's ids that has specific class

I have something very similar to this

<div id="bigBadDiv">
<div class="head">
    blabla
</div>

<div class="contents">
    <div class="1">
        <div id="432" class="item "></div>
        <div id="45" class="item selected"></div>
        <div id="86" class="item "></div>
        <div id="3" class="item "></div>
    </div>

    <div class="2">
        <div id="443" class="item"></div>
        <div id="867" class="item selected"></div>
        <div id="43" class="item selected"></div>
        <div id="98" class="item selected"></div>
    </div>


    <div class="3">
        <div id="423" class="item selected"></div>
        <div id="167" class="item "></div>
        <div id="4453" class="item "></div>
        <div id="944" class="item "></div>
    </div>
</div>

<div class="footer">
    blabla
</div>

I want to select id's only of .selected items and I try to make it with

 $('#bigBadDiv :has(.selected)').css({'backgroundColor':'red'});

but it turns the .contents background to red...
Do you know why is that happening ?
Can you help me select every div.selected and take its id valu? Maybe with some kind of loop .. ?

Upvotes: 0

Views: 1022

Answers (3)

DKSan
DKSan

Reputation: 4197

if you use the standards conform

selected="selected"

you can query the result by

$('*.div').attr('selected')? Do something if : do something if not;

Upvotes: 0

Vincent Robert
Vincent Robert

Reputation: 36120

$('#bigBadDiv .selected').css({'backgroundColor':'red'});

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630409

For the selector, just leave :has() wrapper. For the ID part, you can get an array of ids using .map(), like this:

var ids = $('#bigBadDiv .selected').map(function() { return this.id; }).get();

You can test it out here.

Upvotes: 5

Related Questions