jQuery Selector :has()

Introduction

The :has() selector selects all elements whose children element matches the specified selector.

$(":has(selector)")
Parameter
Optional
Description
selector

Required.

Specifies the element to select.
This parameter accepts any kind of selector

Select 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("border", "solid red");
});//www.java 2  s  .co  m
</script>
</head>
<body>

<p><span>This is a span element inside a p element.</span></p>
<p>This is a p element with no span element.</p>

</body>
</html>



PreviousNext

Related