Search code examples
jquerytextfindmatchcontain

Find if the element contains x but not y (jQuery)


<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.


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");