w3resource
jquery exercises

jQuery: Append a div element with all contents dynamically to the body element

jQuery core : Exercise-6 with Solution

Write jQuery code to append a div element (and all of its contents) dynamically to the body element.

Insert the following code within HTML <body> tag :

<div>
;<h1>JQuery Core</h1></div>

Solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>Append a div element (and all of its contents) dynamically to the body element</title>
</head>
<body>

</body>
</html>

JavaScript Code :

$( "<div><h1>JQuery Core</h1></div>" ).appendTo( "body" );

Live Demo:

See the Pen jquery-core-exercise-6 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Check/uncheck a checkbox input or radio button.
Next: Write a jQuery Code to get a single element from a selection.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming