Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a DOM node

Tags:

javascript

dom

Using just the DOM API, what are all of the different ways I can access a node?

For example, I know I can call document.getElementById("header");. I'd like a complete list of ways to get access to this node.

like image 220
Michael Avatar asked Apr 30 '26 17:04

Michael


2 Answers

"access" can come in two forms. Properties and Methods.

In this list m is a node object (HTML element). Some properties:

  • m.innerHTML - the text
  • m.nodeName -the name
  • m.nodeValue - the value
  • m.parentNode - the parent node
  • m.childNodes - the child nodes
  • m.attributes - the attributes nodes

Some methods:

  • m.getElementById(id) - the element with id
  • m.getElementsByTagName(name) - get all elements by tag name
  • m.appendChild(node) - insert child node to m
  • m.removeChild(node) - remove child node from m

Some "special" ones:

  • document.documentElement - root node of document
  • document.body - direct access to body element tag

Note: there are other proporties such as .length etc for specific use when applicable.

EDIT: A reference to the specification can be found here: http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html

EDIT2: A reference to the level 1 HTML specification here: http://www.w3.org/TR/REC-DOM-Level-1/level-one-html.html

EDIT3: Complete ECMA script binding: http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html

like image 185
Mark Schultheiss Avatar answered May 03 '26 06:05

Mark Schultheiss


The following list contains collections, properties and methods that can be used to access a node. Some of them are element specific, some of them are members of each element, some of them are only available through the document object.

Collections: all, anchors, applets, areas, cells, childNodes, children, elements, embeds, forms, frames, images, links, options, rows, scripts, tBodies

Properties: body, caption, document, documentElement, firstChild, firstElementChild, frameElement, lastChild, lastElementSibling, nextElementSibling, nextSibling, offsetParent, ownerDocument, parentElement, parentNode, previousElementSibling, previousSibling, tFoot, tHead

Methods: getElementById, getElementsByClassName, getElementsByName, getElementsByTagName, getElementsByTagNameNS

You can find further details and examples here: Element handling objects, properties and methods in JavaScript

like image 29
gumape Avatar answered May 03 '26 08:05

gumape



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!