Home >>jQuery Tutorial >parent < child

parent < child

jQuery parent > child Selector

jQuery parent > child selector in jquery is used to selects all elements that are a direct child of the specified element.

Syntax:
("parent > child")

Parameter Values

Parameter Description
parent It is a Required parameter and used to specifies the parent element to be selected
child It is a required parameter and used to specifies the direct child element (of the specified parent element) to be selected
Here is an Example of jQuery parent > child Selector:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div > span").css("background-color", "orange");
});
</script>
</head>
<body>  
<h2>Hello Phptpoint !</h2>
<h4>This div element has two direct child elements, p and span:</h4>
<div style="border:1px solid black;padding:10px;">
<p>This is a paragraph.</p>
<span>This is a text inside a span.</span>
</div>
<h4>This div element has one direct child element, p:</h4>
<div style="border:1px solid black;padding:10px;">
<p>This is a paragraph. <span>This is a span element, inside the paragraph.</span></p>
</div>
</body>
</html>

Output:

Hello Phptpoint !

This div element has two direct child elements, p and span:

This is a paragraph.

This is a text inside a span.

This div element has one direct child element, p:

This is a paragraph. This is a span element, inside the paragraph.


No Sidebar ads