Home >>jQuery Tutorial >jQuery :has() Selector

jQuery :has() Selector

jQuery :has() Selector

jQuery :has() selector in jQuery is used to selects all elements that have one or multiple elements inside of them, that matches the specified selector.

Syntax:
$(":has(selector)")

Parameter Values

Parameter Description
selector It is a Required parameter and used to specifies the element to select.
Here is an Example of jQuery :has() Selector

<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 #97d7fc");
});
</script>
</head>
<body>
<p><span>span element inside a p element.</span></p>
<p>p element with no span element.</p>
</body>
</html>

Output:

span element inside a p element.

p element with no span element.