Javascript DOM features provide the functionality to create new HTML elements such as div, span, paragraph p tag etc. dynamically. document.createElement method of Javascript DOM provides the feature to create new div HTML element or other HTML elements dynamically. You can pass the name of the HTML tag that you want to create to the createElement method of document object. The object created by createElement method can access all the supported properties, functions and methods of document.documentElement method of Javascript. You can access the methods such as setAttribute, style, id and title etc.
document. createElement("HTML element name");
Above syntax of Javascript document. createElement method shows that you can pass the name of HTML Tag you want to insert into the HTML document.
E.g: document.createElement("div")
Above example code will create an instance for new div HTML element in Javascript.
<head>
<title>Javascript create new div</title>
<style type="text/css">
.dynamicDiv {
}
</style>
<script type="text/javascript" language="javascript">
{
divTag.innerHTML = "This HTML Div tag is created using Javascript DOM dynamically.";
</script>
</head>
<input id="btn1" type="button" value="create div" onclick="createDiv();" />
</html>
In the above example Javascript code to create new div HTML tag we have used the appendChild method for document body element. appendChild method takes one parameter as the name of the object of newChild that you want insert into the specified HTML tag like body tag in the above example. You can use the document.getElementById method of javascript passing the id value of any HTML tag along with appendChild method to insert the dynamic new HTML element into the specified id of the HTML tag. In the next tutorial we will learn How to append the div content dynamically using appendChild method of Javascript.
Currently rated 1.0 by 1 people
Tags: javascript, dom, html, javascript create new div, javascript createelement, javascript document object, document object modeling, javascript create div dynamically, dhtml, html div tag, html span tag, javascript innertext, javascript innerhtml, javascript append div contents, javascript javascript appendchild
8/23/2008 8:57:07 PM