Home >>jQuery Tutorial >jQuery contents() Method

jQuery contents() Method

jQuery contents() Method

jQuery contents() method in jQuery is used to returns all direct children, including text and comment nodes, of the selected element displayed by an element.

Syntax:
$(selector).contents()
Here is an Example of jQuery contents() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$(".box").contents().filter("em").wrap("<b/>");
});
});
</script>
</head>
<body>
<div class="box"><em>Hello world! How Are You!</em></div><br>
<button class="btn1">Click me to find all text nodes in div and wrap them</button><br>
</body>
</html>

Output:
Hello world! How Are You!



No Sidebar ads