w3resource

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?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/jquery-exercises/jquery-core-exercise-6.php