pelms pelms - 3 months ago 8
Javascript Question

Select elements whose children don't contain specific elements in jQuery

I want to select all divs on a page whose children don't contain an element with a specific class.

I can select elements whose descendants do contain the class with:


$('div').has('.myClass');


So I just want the inverse of this.

Answer

I'd use ".filter()":

var theDivs = $('div').filter(function() {
  return $(this).find('.myclass').length === 0;
});
Comments