Question

<div>
  <p><a href="#">link</a> some text</p>
  <p><a href="#">link</a></p>
  <p><a href="#">link</a> some text</p>
  <p><a href="#">link</a></p>
</div>

I want to find (and addClass to) the <p> tags that DO NOT contain text, directly inside itself or its children.

Was it helpful?

Solution

This works fine, I did have to empty the text out of one of your paragraphs to test:

var $eles = $('p').filter(function() {
    return $(this).text().length == 0;
});
$eles.addClass("foo");

OTHER TIPS

I suppose You have an error in the problem description, as the a tag always contains text in the example.

As far as I know there is no standard selector allowing to check if there is a direct content, but You could try this:

$('p').each(function(){
 var p = $(this).clone();
 p.children().remove();
 if(p.text().length){
   $(this).addClass('gotcha');
  }
});

Should work if You wish to check only the paragraph insides.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top