DOM (Document Object Model) Flashcards
What is a “model”?
A representation of an existing thing/concept
Which “document” is being referred to in the phrase Document Object Model?
The HTML document being loaded
What is the word “object” referring to in the phrase Document Object Model?
It is referring to the data type: object in JavaScript
What is a DOM Tree?
An element and all of its configurations + its relationship with its siblings/parents represented in the DOM
Give two examples of document
methods that retrieve a single element from the DOM.
document.querySelector()
document.getElementById()
Give one example of a document
method that retrieves multiple elements from the DOM at once.
doucment.querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
To keep the location/reference point saved so one doesn’t have to search the DOM Tree for an element repeatedly
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?
One has to allow the web page to fully load before trying to map it out with a DOM Tree
What does document.querySelector()
take as its argument and what does it return?
It takes a CSS selector as an argument and returns the first element found with that selector.
What does document.querySelectorAll()
take as its argument and what does it return?
Uses a CSS Selector and returns all elements found with that selector in a Node List.
Does assigning the return value of a DOM query actually assign that node to the variable?
No, it only saves the location of where that element is in the DOM Tree. (Tl;dr saves a reference point)
What is the DOM?
It is a recreation of the HTML Document stored as a JavaScript Object which is modeling the content of the HTML Document
What is the purpose of events and event handling?
To run a script whenever certain things happen within or to the browser.
It makes web pages more interactive for the user.
Are all possible parameters required to use a JavaScript method or function?
No, many functions/methods have optional parameters that do not need to be passed through as arguments for the function/method to execute.
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener('event', functionName [, Boolean])
-Boolean optional
What is a callback function?
A function passed as an argument through another function
What object is passed into an event listener callback when the event fires?
An event object that describes the event that occurred
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
A reference to the object in which an event was fired. One can check it by logging it. You could get more information at target documentation
What is the difference between these two snippets of code?
element.addEventListener('click', handleClick) element.addEventListener('click', handleClick())
handleClick is run when the event fires, while handleClick() is run while the page loads