Create a Div Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Div


You can create a <div> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<button onclick="myFunction()">create a DIV element</button>

function myFunction() {/*  ww w. jav a  2s.c o  m*/
    var x = document.createElement("DIV");
    var t = document.createTextNode("This is a div element.");
    x.setAttribute("style", "background-color: red;");


Related Tutorials