The Div
class represents an HTML <div>
element.
Property | Description |
---|---|
align | Sets or gets the align attribute |
The align attribute is not supported in HTML5. Use style.textAlign.
The Div object supports the standard properties and events.
The following code shows how to get a <div> element
by using getElementById()
.
<!DOCTYPE html>
<html>
<body>
<div id="myDIV">a div element</div>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from w w w . j av a2 s . c o m-->
var x = document.getElementById("myDIV").innerHTML;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to create a <div> element by using the document.createElement() method
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- w ww . j a v a 2 s .c o m-->
var x = document.createElement("DIV");
var t = document.createTextNode("a div element");
x.setAttribute("style", "background-color: blue;");
x.appendChild(t);
document.body.appendChild(x);
}
</script>
</body>
</html>
The code above is rendered as follows: