DOM Flashcards
Root Node & Root’s children
document // document has one child node: `html` // ... accessed via: document.documentElement
Properties w/ references to related nodes
.parentNode // all nodes
.parentElement // html element nodes
// the following will traverse // html + text nodes .childNodes .firstChild .lastChild .previousSibling .nextSibling
// only traverse html nodes .children
Properties that explain the node itself (3)
// name of node // for HTML elements, it is the // ... HTML tag in all caps // for text node, value == '#text' // for document node, value == '#document' .nodeName
// text of text-node .nodeValue
// number reflecting type of node—1 for html node, 2 for text, 8 for comments, 9 for document, 10 for .nodeType
Shortcut to reference body
node
document.body
Shortcut to ref every form on page
document.forms
Shortcut to ref every link on page
document.links
Methods for finding DOM nodes (3)
.getElementById(str id)
=> {object|null} if DOM contains 1 node w/ that ID, returns a reference to that node, else, null
.getElementsByTagName(str tag) => {array-like-object} all nodes that have that tag #note: can be used off any node for finer control #example: document.getElementById('header').getElementsByTag('a');
.getElementsByClassName(str class) => {array-like-object} all nodes w/ that class name
.querySelector(str cssSelector) => {obj|null} first node that matches selector, else, null #IE8+ support, but IE8 can only use CSS 2.1 selectors
.querySelectorAll(str cssSelector) => {array} all nodes that match selector #see .querySelector
Working w/ HTML attributes of node
You can add/remove/edit HTML attributes as node-object properties:
node. href = ‘http://google.com’; // add/edit
node. href = null; // remove
Watch out for class
and for
, since they’re reserved words, use className
and htmlFor
instead. Also, this doesn’t seem to work w/ custom attributes or attributes not expected for that element. An alternative:
node. hasAttribute(str name) //=> boolean
node. getAttribute(str name) //=> value
node. setAttribute(str name, str value)
node. removeAttribute(str name)
Editing inner node content (fast way)
.innerText // W3C standard, doesn’t work in IE
.textContent // doesn’t work in Firefox
.innerHTML
Concatenation works, if not wanting to replace:
node.innerHTML += ‘add this’;
Creating nodes
// both create and return node objects // you can then use the usual node props & methods document.createElement(str tag) document.createTextNode(str value)
// or clone an existing node // (doesn't clone event handlers) node.cloneNode() //=> node ref
Determine if node has a child-node
parentNode.contains(obj nodeRef) //=> {bool} if parent contains nodeRef as child
Determine if node is a parent
node.hasChildNodes() //=> {bool} true if child nodes exist, else false
Remove or replace nodes
parentNode.replaceChild(newNode, oldNode) //=> {obj} newNode replaces oldNode, then returns reference to *oldNode* #note: if newNode is already somewhere in the document, it is removed first
parentNode.removeChild(nodeRef) //=> {obj} removes and returns node #note: throws if child isn't a child of parentNode
Add nodes into DOM (slower than .innerHTML)
parentNode.insertBefore(newNode, refNode) //=> add node before refNode, then return ref to new node #note: if refNode is not given, newNode is last child
parentNode.appendChild(nodeRef) //=> add node as last child, then return ref to node #note: if nodeRef is already somewhere in the document, it is removed first and then added as last-child of parent
Add & remove event listener
node.addEventListener(str eventType, func listener, bool useCapture=false)
//=> {undefined} add event listener to node
- keep a reference to the function if you want to remove the event later
- duplicate event listeners are ignore (same eventType + same listener)
- use of this
refers to the node
#compatibility—IE8 will need a shim for event handling methods
// same signature as .addEventListener .removeEventListener(str eventType, func listner, bool useCapture=false) //=> {undefined} removes event listener from node