node ninja
node ninja

Reputation: 32986

How to limit jQuery searches to a certain area

Let's say I have the following.

<div class="foo">
<div>
some text
<div class="bar">
</div>
</div>
</div>

<div class="foo">
<div>
some text
<div class="bar">
some text
</div>
</div>
</div>

I want return all the divs of class "foo" that have "some text" inside the div class "bar." So the second would be returned, but not the second. How can I do that?

Upvotes: 0

Views: 61

Answers (2)

Vigrond
Vigrond

Reputation: 8198

This will do it

$('.foo:has(.bar:not(:empty))')

Make sure there are no characters inside the .bar, even spaces or newlines.

http://jsfiddle.net/Nk4FB/

Upvotes: 1

Paul Grimshaw
Paul Grimshaw

Reputation: 21034

Try this

$("div.bar:contains('some text')").parents(".foo")

Upvotes: 2

Related Questions