DOM: Basic DOM Methods

By Xah Lee. Date: . Last updated: .

Here are the essential Document Object Model methods. Learn to use these and you will know 99% of practical DOM use.

Get Elements

syntaxmeaning
document. getElementById (id)Return a (none-live) element object or null.
document. getElementsByTagName (tag_name)Return a live HTMLCollection.
document. getElementsByClassName (class_value)Return a live HTMLCollection.
document. querySelector (css_selector) Return a non-live HTMLCollection of the first element that matches.
document. querySelectorAll (css_selector)Return a non-live HTMLCollection.

For examples, see:

DOM: Get Elements by ID, Tag, Name, Class, CSS Selector

Element Attribute

Properties for Working with Element's Attributes
syntaxmeaning
node.hasAttribute (attr)Return true or false. This is especially useful for “boolean attributes” that doesn't have value, such as the attribute checked of input tag for checkboxes.
node.getAttribute (attr)Return the value of element's attribute attr. If doesn't exist, returns null. The attr is any of {"id", "href", …}. [see DOM: Get Element's Attribute Value]
node.setAttribute (attr, val)Set the value of the attribute named attr to value val. If the attribute does not exist, it will be created. Return undefined. [see DOM: Set Element's Attribute Value]
node.removeAttribute (attr)Remove the attribute. No error if attribute doesn't exist. Return undefined. [see DOM: Remove Element's Attribute Value]
node.attributes Get all attributes. Return a live Array-Like Object.

For HTML element's standard attributes, you can use a property directly.

Get/Set Standard Attributes by Properties
syntaxmeaning
html_element.attribute_name Return element's attribute's value, or undefined if it doesn't exist.
html_element.attribute_name = valueSet a element's attribute. Return value. If attribute_name is not a standard html attribute, it's not added.
const x = document.getElementsByTagName("a")[0];

x.getAttribute("href");
// is equivalent to
x.href

Using property does not work with XML such as stroke="…" from SVG, or non-standard attributes such as data-xyz="…" from HTML5 Custom Data Attribute.

“class” Attribute

For the class attribute, there's more useful methods.

see DOM: List/Add/Remove Class Attribute

“style” Attribute

For the style attribute, you can use HTML Element properties.

Note, HTML Element properties may not work with XML, e.g. SVG's stroke. [see SVG: Specifying Styles]

syntaxmeaning
node.style.css_attribute Return the attribute value, or undefined if it doesn't exist.
node.style.css_attribute=value Set css attribute. Return value. If attribute is not a standard css attribute, it's not added.

Example: DOM: Change CSS

Get Node {Type, Name, Value}

HTML element is a node. But node also includes white spaces between elements, comments, doctype, and arbitrary XML tags.

syntaxmeaning
node.nodeTypeA integer representing the type of the node node (1 means HTML element, 3 means text node).
node.nodeNameThe name of the node node (either the name of tag (For example, P, DIV, SPAN, …) or "#text")
node.nodeValueThe text value of text node or attribute node. Else, null.

[see DOM: Node Type, Node Name, Node Value]

Navigate Nodes

syntaxmeaning
node.previousSiblingReturn null if none.
node.nextSiblingReturn null if none.
node.firstChildReturn null if none.
node.lastChildReturn null if none.
node.parentNodeReturn null if none.
syntaxmeaning
node.hasChildNodes()Return true or false.
node.childNodesReturn read-only live object NodeList. If no child, the object has length 0.
node.childrenReturn read-only live object HTMLCollection. Value is all non-whitespace children of node.

[see DOM: Navigate DOM Tree]

Create New Nodes

syntaxmeaning
document. createElement (tag_name) Create a new tag_name element node. Return it.
document. createTextNode (str) Create a new text node with the node value of string str.
node.cloneNode(true)Return a copy of node, including all child nodes if argument is true.
node.innerHTMLThe content of node as string. You can read or set this value. [see DOM: Difference Between textContent, innerHTML, innerText, nodeValue]

[see DOM: Create/Insert HTML Element]

{Insert, Remove, Replace} Nodes

syntaxmeaning
node.insertAdjacentElement(position, new_node) [see DOM: .insertAdjacentElement]
syntaxmeaning
p_node.appendChild (new_node) Returns the argument new_node. [see DOM: .appendChild]
p_node.insertBefore (new_node, nodeX) Inserts new_node as a new child of p_node before nodeX.
p_node.removeChild (old_node) Removes the child old_node from p_node.
p_node.replaceChild (new_node, old_node) Replaces the child node old_node of p_node with new_node. If new_node is in the same doc, it'll also be removed.

Examples:

Change Element's Text Content

There are 3 ways to change a element's content.

  1. Treat it as a innerHTML, and use property innerHTML . This is for HTML (not XML), works in all browsers.
  2. Treat it as a text content, and use property textContent . This works well if the content is text and contains no HTML. (if you set value to "<b>bold text</b>", the text won't be bold.)
  3. Treat it as a DOM tree node, and use property nodeValue. This is the most general, and works with XML too.

Example: DOM: Change Element's Content

Detail: DOM: Difference Between textContent, innerHTML, innerText, nodeValue

{Add, Remove} Event Handler

syntaxmeaning
node.addEventListener (event, ƒ, false)Add the function ƒ to the event event
node.removeEventListener (event, ƒ …)Remove the function ƒ that was attached to the event event

[see DOM: Add/Remove Event Handler]

DOM How-To

  1. DOM: Basic DOM Methods
  2. Get Elements by ID, Tag, Name, Class, CSS Selector
  3. Change CSS
  4. Change Node Content
  5. Create/Insert Node
  6. Remove Node
  7. Get Element's Attribute Value
  8. Set Element's Attribute Value
  9. Remove Element's Attribute Value
  10. List/Add/Remove Class Attribute
  11. Add/Remove Event Handler
  12. DOM: Navigate DOM Tree
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc