Selector has

The :has selector selects elements that contain at least one element matching the given selector, not necessarily an immediate descendant. Since :has is not part of the CSS specification, for better performance in modern browsers it is better to use $("your css selector").has(selector/DOM ​​element) instead.

Syntax

This is how we select elements:

javascript$(':has(selector)');

This is how we select elements if we need direct nesting of the selector:

javascript$(':has(>selector)');

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 has,
    which filters the set of selected elements by descendants
  • method filter,
    which filters elements in a set by a given selector