views:

41

answers:

2
<div>
    <div>test</div>
</div>

$("div:contains('test')").css('display','none');

I know I am going to kick myself on this. The problem is that when this runs all divs are hidden due to nesting. How do I make it so that the parent div does not get hidden? I am limited to using 1.2.6

+2  A: 
$("div:contains('test'):not(:has(div))").hide();
reko_t
Legend, that will do the job. thank you!
Dtour
A: 

If you want an elegant solution, define a new selector. Unfortunately, :empty is not sufficient as anything with text node children isn't empty.

$.extend($.expr[':'], {
  leaf: function(elem, i, match) {
    return $(elem).children().length == 0;
  }
});

And then you can do:

$("div:leaf:contains('test')").hide();
cletus