w3resource

jQuery: Appends all spans to the element with specified id

jQuery Fundamental - I : Exercise-26

Using jQuery append all spans to the element with specified id.

<body>
<span> Exercises  </span>
<p  id="myid">jQuery</p>
<button id="button1">Click to see the effect</button>
</body>

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>Using jQuery  append all spans to the element with specified id.</title>
 <style type="text/css">
 button {
 display: block;
 margin: 20px 0 0 0;
 } 
 </style>
 </head>
 <body>
 <span> Exercises </span>
 <p id="myid">jQuery</p>
 <button id="button1">Click to see the effect</button>
 </body>
 </html>

JavaScript Code :

$('#button1').click(function(){ 
$( "span" ).appendTo( "#myid" );
});

Used Methods :

  • .appendTo() : Insert every element in the set of matched elements to the end of the target.

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


Contribute your code and comments through Disqus.

Previous: Using jQuery appends an Element to all paragraphs.
Next: Using jQuery display the checked attribute and property of a checkbox as it changes.

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.