w3resource

jQuery: Find all the text nodes inside a paragraph and wrap them with an italic tag

jQuery Fundamental - II : Exercise-60

Find all the text nodes inside a paragraph and wrap them with an italic tag.

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Find all the text nodes inside a paragraph and wrap them with a italic tag.</title>
</head>
<body>
<p><a href="https://www.w3resource.com/jquery-exercises/">jQuery</a> Exercises, Practice and Solution</p>
<p><a href="https://www.w3resource.com/javascript-exercises/">JavaScript</a>  Exercises, Practice and Solution</p>
</body>
</html>

JavaScript Code :

$( "p" )
.contents()
.filter(function(){
return this.nodeType !== 1;
})
.wrap( "<i></i>" );

See the Pen jquery-fundamental-exercise-60 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Using jQuery find all children with a specified class of each division.
Next: Display a message when the contextmenu event is triggered on the paragraph elements.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.