jQuery Add

jQuery provides several methods to add new HTML elements to the DOM dynamically. These methods make it super easy to insert content before, after, inside, or around existing elements.


jQuery Methods to Add Elements

We will look at four jQuery methods that are used to add new content:

Method

Description

append()

Inserts content at the end of the selected element

prepend()

Inserts content at the beginning of the selected element

after()

Inserts content after the selected element

before()

Inserts content before the selected element

jQuery append() Method


The jQuery .append() method inserts content at the end of the selected HTML element(s).

Example
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").append(" More text added.");
});

$("#btn2").click(function(){
$("ol").append("<li>More item Appended</li>");
});
});
</script>

Try it yourself

jQuery prepend() Method

The jQuery .prepend() method inserts content at the beginning of the selected HTML element(s).

Example
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").prepend("Prepending text at the start.");
});
$("#btn2").click(function(){
$("ol").prepend("<li>Prepended item</li>");
});
});
</script>

Try it yourself

Add Several New Elements With append() and prepend()


You can easily add multiple new elements using both .append() and .prepend() in jQuery. Here's how you can add multiple elements at once using these methods.


In the following example, we create several new elements. The elements are created with text/HTML, jQuery, and JavaScript/DOM. Then we append the new elements to the text with the append() method (this would have worked for prepend() too) :

Example
<script>
$(document).ready(function(){
$("#addContent").click(function(){
// Append several elements to the end of #myDiv
$("#myDiv").append(
"<p>New paragraph 1</p>",
"<a href='https://example.com'>New Link</a>",
);
});
});
</script>

Try it yourself

jQuery after() and before() Methods


The jQuery .after() and .before() methods are used to insert new content after or before the selected elements, respectively.

These methods are useful for inserting content in relation to existing elements without modifying their content directly.

Example
<script>
$(document).ready(function(){
$("#addAfter").click(function(){
// Add new content after the #second paragraph
$("#second").after("<i>New paragraph added after the second one.</i>");

});

$("#addBefore").click(function(){
// Add new content before the #first paragraph
$("#first").before("<i>New paragraph added before the first one.</i>");
});
});
</script>

Try it yourself

Add Several New Elements With after() and before()


You can use .after() and .before() methods to add multiple new elements after or before the selected elements. Just like with .append() and .prepend(), these methods can handle multiple elements in a single call.


In the following example, we create several new elements. The elements are created with text/HTML, jQuery, and JavaScript/DOM. Then we insert the new elements to the text with the after() method (this would have worked for before() too) :

Example
<script>
$(document).ready(function(){
$("#addAfterContent").click(function(){
// Add multiple elements after #myDiv
$("#myDiv").after(
"<p>New paragraph 1 added after the div.</p>",
"<a href='https:example.com'>New Link</p>",
);
});
});
</script>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.