How to add new div using javascriprt?
Thanks in Advance
Srikanth
How to add new div using javascriprt?
Thanks in Advance
Srikanth
You use use
HTMLElement.appendChild().
alternatively you can also try
document.createElement() in combination with document.insertBefore()
Thanks
Lokesh M.
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
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);"
HTML Code:<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 -->
Last edited by Kishore Rajendra; 03-25-2014 at 05:18 AM.