# Web Development > HTML & CSS Adding new div

## Srikanth

How to add new div using javascriprt?

Thanks in Advance
Srikanth

----------


## Lokesh M

You use use 
HTMLElement.appendChild().

alternatively you can also try
document.createElement() in combination with document.insertBefore() 

Thanks
Lokesh M.

----------


## Sandhya.Kishan

The div tag is created dynamically by using document.createElement method of JavaScript.The object created by createElement method can access all the supported properties, functions and methods of document.documentElement method of JavaScript.
Example
Javascript Create Div Element Dynamically

----------


## Kishore Rajendra

To add any element through JavaScript, you can use the method document.createElement("div"). And to set other attributes like "id" or "class" or to add text, this is the way to do it.

Here div2 is being appended inside div1. Simply to add a div, remove the line "div1.appendChild(dElement);"



```
<div id="div1" class="samp">

<script type="text/javascript">

       var dElement = document.createElement("div");

            dElement.setAttribute("id","div2");

            dElement.setAttribute("class","samp2");

            dElement.innerHTML="this is div 2 ";  /* to add text to div2 */

            div1.appendChild(dElement);

</script>

</div>  <!-- div1 ending -->
```

----------

