HTML DOM Attributes
Attributes are values of nodes (HTML elements) that you can get or set.
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 innerHTML Property
The easiest way to get the content of an element is to use the innerHTML property.
The innerHTML property is useful for getting or replacing the content of HTML elements.
Example
The following code gets the element with id="intro"
The innerHTML of an element:
Example
<!DOCTYPE html><html>
<head>
<meta charset="utf-8" head>
<body>
<p id="intro">Hello World!
p
<
script
var txt=document.getElementById("intro").innerHTML; document.write(txt);
script
body
html
Try it »
In the example above, getElementById is a method, while innerHTML is a property.
![]() |
The innerHTML property can be used to get or change any HTML element, including and . |
|---|
The nodeName property specifies the name of a node.
- nodeName is read-only
- The nodeName of an element node is the same as its tag name
- The nodeName of an attribute node is the same as its attribute name
- The nodeName of a text node is always #text
- The nodeName of a document node is always #document
Note: nodeName always contains the uppercase tag name of the HTML element.
The nodeValue Property
The nodeValue property specifies the value of a node.
- The nodeValue of an element node is undefined or null
- The nodeValue of a text node is the text itself
- The nodeValue of an attribute node is the attribute value
Get the Value of an Element
The following example retrieves the
text node value of the tag:
Example
<!DOCTYPE html><html>
<head>
<meta charset="utf-8" head>
<body>
<p id="intro">Hello World!
p
<
script
x=document.getElementById("intro"); document.write(x.firstChild.nodeValue);
script
body
html
Try it »
The nodeType Property
The nodeType property returns the type of a node. nodeType is read-only.
The more important node types are:
| Element type | NodeType |
|---|---|
| Element | 1 |
| property | 2 |
| Text | 3 |
| Notes | 8 |
| Document | 9 |
