/    /  Javascript DOM- methods

Javascript DOM- methods

 

The important methods of document object:

 

Method                                              Description

write(“string”)                         writes the given string on the document.

writeln(“string”)                      writes the given string on the document with a newline character at the end.

getElementById()                    returns all element having the given id value.

getElementsByName()            returns the elements having the given name value.

getElementsByTagName()      returns the elements having the given tag name.

getElementsByClassName()   returns the elements having the given class name.

 

  • DOM(Document Obeject Model) methods are actions you can perform (on HTML Elements).
  • DOM properties are values (of HTML Elements) that you can change or set.

 

Example:

 

<!DOCTYPE html>
<html>
<body>
<h2>My First Page</h2>
<p id="i2t"></p>
<script>
document.getElementById("i2t").innerHTML = "Welcome to I2tutorials";
</script>
</body>
</html>

 

OUTPUT:

 

My First Page
Welcome to I2tutorials

 

In the example above,

  1. getElementById is a method.
  2. innerHTML is a property.

 

  • A most common way to access an HTML element is to use the “id” of the element.

In the example above,

            getElementById method used id=”demo” to find the element.

  • The easiest way to get the content of an element using the innerHTML
  • The innerHTML property is useful for getting or replacing(change or set) the content of HTML elements.