create div element dynamically by using javascript

In this tutorial you will learn how to create div element dynamically by using javascript. We will do this by utilizing the DOM features of javascript. Let's have a look over example given below

Create div element dynamically by using javascript

<html xmlns="">

<title>Create div element dynamically using javascript</title>

<style type="text/css">

color: #999;

padding: 10px;

width: 250px;

height: 150px;

border: solid 1px #666;



<script type="text/javascript" language="javascript">

function CreateDivElement() {

var divElement = document.createElement("div"); = "myDiv";

divElement.className = "myDivClass";

divElement.innerHTML = "Hello World!";






<input id="myBtn" type="button" value="Click Me" onclick="CreateDivElement();" />



And if you want to want to create div element inside any other html element of page body then nothing to worry. Let's suppose we have a div in our page which id is testDiv like given below
<div id="testDiv">
and you want to create div inside this testDiv. Just you have to change the last line of CreateDivElement() function from


and that's it. I hope you will like this tutorial.

Happy Coding, Keep Coding