contents() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:contents

Description

The contents() method returns all direct children, including text and comment nodes, of the selected element.

The following code shows how to Find all the text nodes inside a <div> element and wrap them with a <b> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("div").contents().filter("em").wrap("<b/>");
    });//w  w w  . ja v a 2 s .co  m
});
</script>
</head>
<body>

<div><em>Hello world! What a beautiful day!</em></div>

<button>test</button><br>

</body>
</html>

Related Tutorials