This post will discuss how to dynamically create a <div> element in JavaScript and jQuery.

1. Using JavaScript

In vanilla JavaScript, you can use the native createElement() method to create an HTML <div> element and the appendChild() method to append the <div> element to another container.

JS


HTML


CSS



Edit in JSFiddle

2. Using jQuery

With jQuery, you can use the .append() method to append the <div> element at the end of the specified container.

JS


HTML


CSS



Edit in JSFiddle

 
This can be shortened to:

JS


HTML


CSS



Edit in JSFiddle

 
Here’s alternate version using .appendTo() method:

JS


HTML


CSS



Edit in JSFiddle

That’s all about dynamically creating a div element in JavaScript and jQuery.