The has method

The has method filters the set of selected elements, leaving only those that have descendants that match the given selector.

Syntax

Filter by descendants:

javascript.has(descendant selector);

Here's how you can filter a set of selected elements, leaving only the one that contains a given DOM element inside:

javascript.has(DOM element);

Example

Let's select only those paragraphs that have the tag b inside and put the text '!!!' at the end of them:

html<p>text</p>
<p>text</p>
<p><b>bold</b> text</p>
<p><b>bold</b> text</p>
javascript$('p').has('b').append('!!!');

HTML the code will look like this:

html<p>text</p>
<p>text</p>
<p><b>bold</b> text!!!</p>
<p><b>bold</b> text!!!</p>

See also

  • method filter,
    which filters elements in a set by a given selector
  • method find,
    which searches for elements within those already found