document
contains the DOMnodeType
1
document.ELEMENT_NODE
to make this easierdocument.getElementsByTagName("a")
document.getElementsByClassName("selected")
document.getElementById("gertrude")
new methods in JavaScript (IE9+) make selecting elements even easier
use CSS selectors to select elements
with these two, you don't need any of the previous methods
document.querySelector()
- returns the first matching elementdocument.querySelectorAll()
- returns an array like object, a NodeList, of all matching elements.getElementsByTagName()
and .getElementsByClassName()
return a live list
.querySelectorAll()
returns a static list
Array.from()
is a new method for making an array from an array like object
let arrayish = { 0: 'one', 1: 'two', length: 2 };
let array = Array.from(arrayish);
These are different than the textbook but only select elements, skipping other nodes.
.children
.firstElementChild
.lastElementChild
.nextElementSibling
.previousElementSibling
.parentElement
.remove()
.replaceWith()
.appendChild()
.insertBefore()
To make it easier to work with the content of elements we have two properties:
.textContent
- only gets or sets text content of the element.innerHTML
- contained elements are represented as tagsdocument.createElement()
document.createTextNode()
href
is .href
, id
is .id
getAttribute()
setAttribute()
data-
class
Attributeclass
is a reserved word in JavaScriptclassName
insteadclassList
.add()
, .remove()
, .toggle()
, .contains()
style
property contains properties for every possible styleconst para = document.getElementById('para');
console.log(para.style.color);
para.style.color = 'magenta';
font-family
becomes fontFamily
style
property applies directly to the elementdocument