JS & the DOM Flashcards
Why do we log things to the console?
So you can see what the data looks like; so you can see what’s going on
What is a “model”?
Relation to DOM (document object model)
-> The DOM is a representation/model/replica of the HTML document, it is not the actual HTML document;
Which “document” is being referred to in the phrase Document Object Model?
The html document
What is the word “object” referring to in the phrase Document Object Model?
The DOM represents all the information in HTML document (elements, text, attributes, etc) as JavaScript objects
What is a DOM Tree?
It is a model of the HTML document created by the browser and stored in memory. The DOM tree is a tree data structure of nodes
Give two examples of document methods that retrieve a single element from the DOM.
- document.querySelector( )
2. document.getElementById( )
Give one example of a document method that retrieves multiple elements from the DOM at once.
document.querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
Querying for an element using document.querySelector() method takes time/processing resources because you are traversing the DOM, looking for a node
If you have the node saved in a variable and want to do something to it, you don’t need to query the DOM for it again
What console method allows you to inspect the properties of a DOM element object (and JavaScript objects in general)?
console.dir( )
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
the DOM has to fully load before you can access / manipulate it with JavaScript
What does document.querySelector() take as its argument and what does it return?
It takes a CSS selector (as a string) as an argument and returns the first DOM node that matches the selector (or null if no matching node found)
What does document.querySelectorAll() take as its argument and what does it return?
It takes a CSS selector (as a string) as argument and returns a static nodelist of nodes that match the selector
What is the purpose of events and event handling?
They let your webpage respond/react to user actions - with events you can listen for stuff and then do stuff in response to it
Are all possible parameters required to use a JavaScript method or function?
No. Just because a function is given parameter doesn’t mean it has to do anything with it.
What method of element objects lets you set up a function to be called when a specific type of event occurs?
element.addEventListener()
What is a callback function?
A function passed into another function as an argument. The outer function calls the callback function later when it needs it.
What object is passed into an event listener callback when the event fires?
The event object
What is the event.target?
The element that received the event; the element that was interacted with.
What is the className property of element objects?
It is the class attribute of that html element; access or update the class attribute using this property