jQuery has()

Introduction

Return all <p> elements that have a <span> element inside of them:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p").has("span").css("background-color", "yellow");
});//  w w  w .ja v a 2s.  c  o  m
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p>My <span>name</span> is CSS.</p>
<p>This is a <span>test</span>.</p>
<p>This is another test.</p>

</body>
</html>

The has() method selects elements by contained elements.

To select multiple elements, use comma.

Syntax

$(selector).has(element)
Parameter
Optional
Description
element

Required.

a selector expression or
an element to match elements against



PreviousNext

Related