document
- refers to entire document, hashead
andbody
propertiesdocument.documentElement
- refers to object representing<html>
tag; root of the DOM treefoo.childNodes
- refers toNodeList
of child nodes of nodefoo
foo.firstChild
- refers to first child of nodefoo
(ornull
)foo.lastChild
- refers to last child of nodefoo
(ornull
)foo.previousSibling
,foo.nextSibling
- refer to adjacent nodes (ornull
)foo.children
- refers to only element childrenfoo.getElementsByTagName("bar")
- refers to an array-like object of elements with the tag namebar
in the subtree rooted atfoo
foo.getElementById("bar")
- refers to the object with idbar
in the subtree rooted atfoo
foo.getElementsByClassName("bar")
- refers to an array-like object of elements with the class namebar
in the subtree rooted atfoo
foo.appendChild(bar)
- appendbar
to end offoo
'schildNodes
foo.insertBefore(newBar, oldBar)
- insertnewBar
beforeoldBar
in subtree rooted atfoo
foo.replaceChild(newBar, oldBar)
- replaceoldBar
bynewBar
in subtree rooted atfoo
document.createTextNode("foo")
- returns a text node with textfoo
that can be inserted into the documentdocument.createElement("foo")
- returns an element with tagfoo
foo.getAttribute("bar")
- returns value of attributebar
for elementfoo
foo.setAttribute("name", "value")
- setname
attribute offoo
element tobar
foo.offsetWidth
/foo.offsetHeight
- returns width/height of elementfoo
foo.clientWidth
/foo.clientHeight
- returns width/height of elementfoo
ignoring bordersfoo.getBoundingClientRect
- returns{top, bottom, left, right}
indicating pixel properties offoo
foo.pageXOffset
/foo.pageYOffset
- returns scroll positionfoo.style
- refers to object that has properties for all possible style properties (hyphenated names are replaced with camelCase names)foo.querySelectorAll("bar")
- returns aNodeList
containing all elements that matchbar
(non live version). UsequerySelector
(without theAll
) to get the first element that matches ornull
(if none match)window
- object for registering handlers on entire windowfoo.addEventListener("baz", bar)
- call functionbar
for eventbaz
on elementfoo
foo.removeEventListener("baz", bar)
- remove functionbar
for eventbaz
on elementfoo
- Event object - holds additional information about the event, for example
let button = document.querySelector("button");
button.addEventListener("mousedown", event => {
if (event.button == 0) {
console.log("Left button");
} else if (event.button == 1) {
console.log("Middle button");
} else if (event.button == 2) {
console.log("Right button");
});
event.type
- stores string identifying eventfoo.stopPropogation()
- stop event propogation to parent elementsfoo.target
- refers to node where event originatedkeyup
,keydown
- events received on key pressesevent.ctrlKey
/event.shiftKey
/event.altKey
etc - check whether key is being held downmousedown
/mouseup
- events that occur on mouse clicks on DOM nodes immediately below the mouse pointer when the event occursclick
- event fires on the most specific node that contained both the press and the release of the buttondblclick
- event fires when two clicks happen close togetherclientX
/clientY
- refers to coordinates of the place where a mouse event happened relative to top-left corner of the windowmousemove
- event fired each time mouse movesfoo.preventDefault()
- prevent default action for eventfoo
foo.buttons
- refers to mouse buttons being held down