w3resource

jQuery: Create a paragraph element with some text and append it to the end of the document

jQuery Fundamental - I : Exercise-3

Create a paragraph element with some text and append it to the end of the document body using jQuery.

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>Create a paragraph element with some text and append it to the end of the document body using jQuery.</title>
</head>
<body>
<p>jQuery</p>
</body>
</html>

JavaScript Code :

$( "p" ).add( "<span>Exercises</span>" ).appendTo( document.body );

Used Methods :

  • .add(elements) : Create a new jQuery object with elements added to the set of matched elements.
  • .appendTo() : Insert content, specified by the parameter, to the end of each element in the set of matched elements.

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


Contribute your code and comments through Disqus.

Previous: Set the background color red of the following elements using jQuery.
Next: Using jQuery add the previous set of elements on the stack to the current set.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter