DOM Flashcards
What is a “model”?
A representation of something that focuses on the relevant characteristics of said thing
Which “document” is being referred to in the phrase Document Object Model?
The html element being loaded by the browser
What is the word “object” referring to in the phrase Document Object Model?
The JavaScript object that the browser creates to model the html element
What is a DOM Tree?
The hierarchy of element nodes in the document object
Give two examples of document methods that retrieve a single element from the DOM.
.querySelector(), .getElementById()
Give one example of a document method that retrieves multiple elements from the DOM at once.
.querySelectorAll(‘selector’)
Why might you want to assign the return value of a DOM query to a variable?
It allows the script to reuse the node without having to query for it every time, saving on computation
What console method allows you to inspect the properties of a DOM element object?
console.dir()
Why would a script tag need to be placed at the bottom of the HTML content instead of at the top?
Placing it at the top would mean all of the script is run before any of the body elements are loaded
What does document.querySelector() take as its argument and what does it return?
argument: selector string
return: first element node with the matching selector
What does document.querySelectorAll() take as its argument and what does it return?
argument: selector string
return: a NodeList of all element nodes that match the selector
What is the purpose of events and event handling?
They allow the web page to react to situational events that occur in real time
What method of element objects lets you set up a function to be called when a specific type of event occurs?
.addEventListener(‘event’, function)
What is a callback function?
A function passed as an argument into another function, which can then be used by the other function
What object is passed into an event listener callback when the event fires?
An event object containing the details (e.g. mouse location) of the event, that was created by the browser right when the event fires
What is the event.target?
The most immediate element node that was interacted with when the event triggers
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The former uses a callback function while the latter calls the function handleClick immediately when the line is read
What is the className property of element objects?
It is the string value of the element’s class attribute
How do you update the CSS class attribute of an element using JavaScript?
Set element.className to a new string value