Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM Methods

HTML DOM Methods


HTML DOM methods are the actions we can perform on nodes (HTML elements).

HTML DOM properties are the values we can set and modify on nodes (HTML elements).


Programming Interface

The HTML DOM can be accessed through JavaScript (and other programming languages).

All HTML elements are defined as objects, and the programming interface consists of object methods and object properties.

A method is an action you can perform (such as adding or modifying an element).

A property is a value you can get or set (such as the name or content of a node).


The getElementById() method

The getElementById() method returns the element with the specified ID:

Example

var

element

=

document

.

getElementById

(

"

intro

"

)

;

Try it »


HTML DOM Objects - Methods and Properties

Some commonly used HTML DOM methods:

Some commonly used HTML DOM properties:

You will learn more about properties in the next chapter of this tutorial.


Objects in Real Life

A person is an object.

A person's methods might be eat(), sleep(), work(), play(), and so on.

All people have these methods, but they execute them at different times.

A person's properties include name, height, weight, age, gender, and so on.

All people have these properties, but their values differ from person to person.


Some DOM Object Methods

Here are some common methods you will learn in this tutorial:

Method Description
getElementById() Return the element with the specified ID.
getElementsByTagName() Return a node list (collection/node array) containing all elements with the specified tag name.
getElementsByClassName() Return a node list containing all elements with the specified class name.
appendChild() Add a new child node to the specified node.
removeChild() Remove a child node.
replaceChild() Replace a child node.
insertBefore() Insert a new child node before the specified child node.
createAttribute() Create an attribute node.
createElement() Create an element node.
createTextNode() Create a text node.
getAttribute() Return the specified attribute value.
setAttribute() Set or modify the specified attribute to the specified value.