DOM Flashcards

1
Q

Root Node & Root’s children

A
document
// document has one child node: `html`
// ... accessed via:
document.documentElement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Properties w/ references to related nodes

A

.parentNode // all nodes
.parentElement // html element nodes

// the following will traverse
// html + text nodes
.childNodes
.firstChild
.lastChild
.previousSibling
.nextSibling
// only traverse html nodes
.children
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Properties that explain the node itself (3)

A
// 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Shortcut to reference body node

A

document.body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Shortcut to ref every form on page

A

document.forms

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Shortcut to ref every link on page

A

document.links

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Methods for finding DOM nodes (3)

A

.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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Working w/ HTML attributes of node

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Editing inner node content (fast way)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Creating nodes

A
// 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Determine if node has a child-node

A
parentNode.contains(obj nodeRef)
//=> {bool} if parent contains nodeRef as child
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Determine if node is a parent

A
node.hasChildNodes()
//=> {bool} true if child nodes exist, else false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Remove or replace nodes

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Add nodes into DOM (slower than .innerHTML)

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Add & remove event listener

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Trigger event manually

A
node.displayEvent(obj event)
=>  {bool} false if any event-handler called .preventDefault, true otherwise
#compatibility—IE8 needs a shim, Event constructor (from example) won't work in IE at all
#example:
ref.displayEvent(new Event('click'));
17
Q

Stop default handling

A

// finish
.stopPropagation
.preventDefault

18
Q

Finish event API

https://developer.mozilla.org/en-US/docs/Web/API/Event

A

true