DOM: Basic DOM Methods

By Xah Lee. Date: . Last updated: .

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

Get Elements

document. getElementById (id)Return a (none-live) element object or null. The id should be a string.
document. getElementsByTagName (tag_name)Return a live HTMLCollection. The tag_name are {"div", "span", "p", …}.
document. getElementsByClassName (class_value)Return a live HTMLCollection. The class_value should be a string.
document. querySelectorAll (css_selector)Return a non-live HTMLCollection. The css_selector should be a string of CSS Selector Syntax.

For examples, see:

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

Element Attribute

Properties for Working with Element's Attributes
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. [see DOM: Set Element's Attribute Value]
node.removeAttribute (attr)Remove the attribute.
node.attributesReturn a live Array-Like Object that are all the attributes. The result object can be indexed by a number of attribute name.

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

Get/Set Standard Attributes by Properties
html_element.attribute_nameGet a element's attribute's value.
html_element.attribute_name = valueSet a element's attribute.
const myel = document.getElementsByTagName("a")[0];

// is equivalent to

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 convenient methods:

see DOM: List/Add/Remove Class Attribute

「style」 Attribute

For the style attribute, there's more convenient properties, but may not work with XML.

syntaxmeaning a element's CSS attribute's value. a element's CSS attribute.

Example: DOM: Change CSS

Get Node {Type, Name, Value}

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

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.
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

document. createElement (tag_name) Create and return new tag_name element node.
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

node.insertAdjacentElement(position, new_node) Move new_node to position relative to node. Can insert {before, after} as sibling, or as {first, last} child. [see DOM: .insertAdjacentElement]
p_node.appendChild (new_node)Move new_node to become last child of p_node, if both nodes are in the same document. Else, just append. It 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.


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

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]

Basic 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. List/Add/Remove Class Attribute
  10. Add/Remove Event Handler
  11. DOM: Navigate DOM Tree
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon