:has() Selector - Javascript jQuery Selector

Javascript examples for jQuery Selector:has

Description

The :has() selector selects elements who have the specified elements inside.

Syntax

Parameter Description
selector Required. Specifies the element to select.

The following code shows how to select all <div> elements that have no h1 element

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("div:not(:has(h1))").css("background-color", "yellow");
});// ww w.  j  ava2 s  .  c  o m
</script>
</head>
<body>

<div>
  <h1>This is a heading (h1)</h1>
  <p>This is a paragraph.</p>
</div>

<div>
  <h2>This is a h2</h2>
  <p>This is a paragraph.</p>

    <div>
      <h1>This is a heading (h1)</h1>
      <p>This is a paragraph.</p>
    </div>

</div>

<div>
  <h3>This is a h3</h3>
  <p>This is a paragraph.</p>
</div>

</body>
</html>

Related Tutorials