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:
- getElementById(id) - get the node (element) with the specified id
- appendChild(node) - insert a new child node (element)
- removeChild(node) - remove a child node (element)
Some commonly used HTML DOM properties:
- innerHTML - the text value of a node (element)
- parentNode - the parent node of a node (element)
- childNodes - the child nodes of a node (element)
- attributes - the attribute nodes of a node (element)
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. |