Home >>Javascript Tutorial >Javascript Document Object Model(DOM)
The document object model in JavaScript generally represents the whole document of HTML. Whenever a HTML document is not loaded in the browser it becomes a document object, in other words; it is the root element representing the HTML document. It possess properties and methods and with the help of document object, dynamic content can be added to the webpage.
Please note that window.document is similar as document.
Here are the properties of document object that can be modified and accessed by the document object in JavaScript:
By the methods of the document object, a document can be accessed and changes can be made. Here are some of the most important methods of document:
Method | Description |
---|---|
write("string") | This method writes the given string on the document. |
writeln("string") | This method writes the given string on the document with newline character at the end. |
getElementById() | This method returns the element having the provided id value. |
getElementsByName() | This method returns all the elements having the given name value. |
getElementsByTagName() | This method returns all the elements having the given tag name. |
getElementsByClassName() | This method returns all the elements having the given class name. |
Here is an example to let you understand the document object with these following parameters included:
In this example, the value of the input text by the user will be derived and document.form1.value is being used to get the value of the name field. All the terms and parameters are explained above.
Here is the example of a document object that prints name with a welcome message:
<script type="text/javascript"> function printvalue() { var name=document.form1.name.value; alert("Welcome: "+name); } </script> <form name="form1"> Enter Name:<input type="text" name="name"/> <input type="button" onclick="printvalue()" value="print name"/> </form>