1. jQuery Add Content – DOM Manipulation

jQuery makes it simple to add new content to the DOM without reloading the page. Whether you need to insert text, HTML elements, or entire sections dynamically, these DOM modification methods provide flexible ways to inject content before, after, or inside existing elements. In this tutorial, you will learn how to use the following methods to add content with jQuery in a clear, easy-to-understand way.

1.1. Key Methods for Adding Content

  • .append(): Insert content at the end of selected elements.
  • .appendTo(): Add elements to the end of target elements.
  • .prepend(): Insert content at the beginning of selected elements.
  • .prependTo(): Add elements to the beginning of target elements.
  • .after(): Insert content immediately after selected elements.
  • .insertAfter(): Add elements to immediately follow target elements.
  • .before(): Insert content immediately before selected elements.
  • .insertBefore(): Add elements to immediately precede target elements.

2. Add Content with append() Method

The append() method in jQuery adds content to the end of selected elements. You can use it to insert plain text, HTML tags, or other elements. This is useful when you want to add new items like paragraphs, list items, or divs inside an existing container.

Example

$(function(){
	$("#addItem").on("click", function(){
		$("#list").append("<li>New list item</li>");
	});
});

Try in CodeLab

2.1. jQuery append() Method with Callback

You can pass a callback function to the append() method to compute content dynamically based on index or current HTML. The callback receives the element’s index and its current HTML.

Example with Callback

 
$(function(){
	let count = 1;
	$("#addComment").click(function(){
		$("#commentBox").append(function(){
			count++;
			return `<div class="comment">New comment #${count}</div>`;
		});
	});
});

Try in CodeLab

3. Add Content with appendTo() Method

The appendTo() method accepts existing jQuery objects or newly created elements and inserts them at the end of the selected element(s). It works like append(), but with reversed syntax.

Add New Element

$(document).ready(function(){
	let count = 1;
	$("#addBox").click(function(){
		$("<div class='box'>Box " + count++ + "</div>").appendTo("#container");
	});
});

Try in CodeLab

4. Add Content with prepend() Method

Use the prepend() method to insert content at the beginning of each matched element. This is handy for jQuery prepend scenarios where you need new items to appear first.

Example

$(function(){
	$("#addItem").on("click", function(){
		$("#list").prepend("<li>New list item</li>");
	});
});

Try in CodeLab

4.1. jQuery prepend() Method with Callback

Like append(), prepend() also accepts a callback to generate content dynamically. The callback arguments are index and current HTML.

Example with Callback

 
$(function(){
	let count = 4;
	$("#addItem").click(function(){
		$("#list").prepend(function(){
			return "<li>Item " + count++ + "</li>";
		});
	});
});

Try in CodeLab

5. Add Content with prependTo() Method

The prependTo() method accepts existing jQuery objects or newly created elements and inserts them at the start of the selected element(s). It’s the reverse of prepend(), great for reordering workflows where you need to insert elements at the beginning.

Example

$(document).ready(function(){
	let count = 1;
	$("#addBox").click(function(){
		$("<div class='box'>Box " + count++ + "</div>").prependTo("#container");
	});
});

Try in CodeLab

6. Add Content with after() Method

The jQuery after() method allows us to insert content immediately following each element in the matched set.

Example

$(document).ready(function(){
	$(".item").after("<div class='note'>Note added</div>");
});

Try in CodeLab

6.1. jQuery after() Method with Callback

The after() method also accepts a callback to compute the HTML string dynamically. It takes index and current HTML.

Example with Callback

 
$(document).ready(function(){
	$("#addNote").click(function(){
		$(".item").after(function(index){
			return "<div class='note'>Note after Item " + (index + 1) + "</div>";
		});
	});
});

Try in CodeLab

7. Add Content with insertAfter() Method

The insertAfter() method accepts existing jQuery objects or newly created elements and inserts them right after the selected element(s).

Example

$(document).ready(function(){
	let count = 1;
	$("#addBox").click(function(){
		const newBox = $("<div class='box'>Box " + count++ + "</div>");
		newBox.insertAfter(".box:last");
	});
});

Try in CodeLab

8. Add Content with before() Method

The before() method accepts existing jQuery objects or newly created elements and inserts them right before the selected element(s).

Example

$(document).ready(function(){
	$("#subscribeBtn").click(function(){
		const email = $("#email").val().trim();
		if (email) {
			$("#email").before('<div class="notice">You are subscribed with: <strong>' + email + '</strong></div>');
			$(this).prop("disabled", true).text("Subscribed");
			$("#email").prop("disabled", true);
		}
	});
});

Try in CodeLab

8.1. jQuery before() Method with Callback

You can also pass a callback to before() to create content programmatically. The callback gets index and current HTML.

Example with Callback

 
$(document).ready(function(){
	$("#subscribeBtn").click(function(){
		const email = $("#email").val().trim();
		if (email) {
			$("#email").before(function(){
				return '<div class="notice">You are subscribed with: <strong>' + email + '</strong></div>';
			});
			$(this).prop("disabled", true).text("Subscribed");
			$("#email").prop("disabled", true);
		}
	});
});

Try in CodeLab

9. Add Content with insertBefore() Method

The insertBefore() method moves the matched elements so they appear directly before the specified target, complementing before() for "Add to Cart" or other dynamic UI tasks.

Example

$(document).ready(function(){
	$("#addPromo").click(function(){
		const promo = $('<div class="promo">🎉 New Deal: Subscribe now and save 30%!</div>');
		promo.insertBefore(".main-content");
		$(this).prop("disabled", true).text("Promo Added");
	});
});

Try in CodeLab

Pro Tip:

When dynamically adding content, jQuery methods like append(), prepend(), before(), and after() offer callback support, enabling flexible content generation based on each matched element.

Conversely, appendTo(), prependTo(), insertBefore(), and insertAfter() require pre-defined content or elements, lacking callback functionality, yet remain useful for direct content insertion.

💡 Do You Know?

The same append(), prepend(), before(), after(), appendTo(), and insertBefore()/insertAfter() methods used for adding content can also move existing elements in the DOM.
Give Us Your Feedback
OR
If You Need Any Help!
Contact Us