From http://www.w3schools.com (Copyright Refsnes Data)

HTML DOM Properties and Methods

previous next

Properties and methods define the programming interface to the HTML DOM.


Programming Interface

The DOM models HTML as a set of node objects. The nodes can be accessed with JavaScript or other programming languages. In this tutorial we use JavaScript.

The programming interface to the DOM is defined by a set standard properties and methods.

Properties are often referred to as something that is (i.e. nodename is "p").

Methods are often referred to as something that is done (i.e. delete "p").


HTML DOM Properties

These are some typical DOM properties:

Note: In the list above, x is a node object (HTML element).


HTML DOM Methods

Note: In the list above, x is a node object (HTML element).


innerHTML

The easiest way to get or modify the content of an element is by using the innerHTML property.

innerHTML is not a part of the W3C DOM specification. However, it is supported by all major browsers.

The innerHTML property is useful for returning or replacing the content of HTML elements (including <html> and <body>), it can also be used to view the source of a page that has been dynamically modified.

Example

The JavaScript code to get the text from a <p> element with the id "intro" in a HTML document:

txt=document.getElementById("intro").innerHTML

After the execution of the statement, txt will hold the value "Hello World!"

Explained:

In the example above, getElementById is a method, while innerHTML is a property.

Try it yourself


childNodes and nodeValue

The W3C DOM specified way of getting to the content of an element works like this:

The JavaScript code to get the text from a <p> element with the id "intro" in a HTML document:

txt=document.getElementById("intro").childNodes[0].nodeValue

After the execution of the statement, txt will hold the value "Hello World!"

Explained:

In the example above, getElementById is a method, while childNodes and nodeValue are properties.

Try it yourself

In this tutorial we will mostly use the innerHTML property. However, learning the method above is useful for understanding the tree structure of the DOM and dealing with XML files.


previous next

From http://www.w3schools.com (Copyright Refsnes Data)