Joren
Joren

Reputation: 3287

Selecting all but children

I've got a HTML structure like this:

<div class="foo">
    <ul>
        <li>
            <a href="link.bar">Good</a>
            <ul class="children">
                <li>
                    <a href="otherlink.baz">Bad</a>
                </li>
            </ul>
        </li>
    </ul>
</div>

Now I want to select the "good" a without selecting the "bad" a (using jQuery). I've tried .foo ul:not(.children) li a but that doesn't work (obviously). Would it be possible to achieve in a one-liner?

Fiddle

Upvotes: 0

Views: 53

Answers (3)

Niklas
Niklas

Reputation: 30002

You could look for direct descendant .foo > ul > li > a

Upvotes: 2

Tauseef
Tauseef

Reputation: 2052

in this particular scenario that you gave . it can be done by one lineras follows

$(a ,'.foo').first();

I hope it helps. If it does not work, do let me know and I'll delete my answer. thanks.

Upvotes: 0

Techsin
Techsin

Reputation: 532

Do this, it's one of the ways...

var good= $("a[href='link.bar']")...

http://api.jquery.com/attribute-equals-selector/

Upvotes: 0

Related Questions