Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selector for an element MISSING a child element

I am needing help forming a jquery selector to return elements which are missing a particular child element.

Given the following HTML fragment:

    <div id="rack1" class="rack">
    <span id="rackunit1" class="rackspace">
        <span id="component1">BoxA</span>
        <span id="label1">Space A</span>
    </span>
    <span id="rackunit2" class="rackspace">
        <span id="label2">Space B</span>
    </span>
    <span id="rackunit3" class="rackspace">
        <span id="component2">BoxA</span>
        <span id="label3">Space C</span>
    </span>
</div>
<div id="rack2" class="rack">
    <span id="rackunit4" class="rackspace">
        <span id="component3">BoxC</span>
        <span id="label4">Space D</span>
    </span>
    <span id="rackunit5" class="rackspace">
        <span id="label5">Space E</span>
    </span>
    <span id="rackunit6" class="rackspace">
        <span id="component4">BoxD</span>
        <span id="label6">Space F</span>
    </span>
</div>

Find for me the rackunit spans with NO component span. Thus far I have: $(".rack .rackspace") to get me all of the rackunit spans, not sure how to either exclude those with a component span or select only those without one...

like image 542
Cos Callis Avatar asked Jan 23 '13 15:01

Cos Callis


1 Answers

I guess the following should work:

$(".rack .rackspace:not(:has(span[id^=component]))"). ...

DEMO: http://jsfiddle.net/WbCzj/

like image 181
VisioN Avatar answered Sep 30 '22 01:09

VisioN