DOM Flashcards
Why do we log things to the console?
To verify that the result is what we’re expecting
What is a “model”?
A representation/recreation of the actual thing. In this case, a DOM tree
Which “document” is being referred to in the phrase Document Object Model?
The HTML/web page
What is the word “object” referring to in the phrase Document Object Model?
JavaScript object
What is a DOM Tree?
Data representation of all the objects in the document. Any of the element and all of its child and content
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(), getElementsByClassName(), getElementsByTagName()
Why might you want to assign the return value of a DOM query to a variable?
If you want to reuse the query
What console method allows you to inspect the properties of a DOM element object?
console.dir()
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
The browser needs to parse all of the elements in the HTML page before JavaScript can access them
What does document.querySelector() take as its argument and what does it return?
It will take a CSS selector and returns the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
It will take a CSS selector and returns all matching elements in a node list
Why do we log things to the console?
To verify that the result is what we’re expecting
What is the purpose of events and event handling?
So our script can respond to certain events/user inputs
Are all possible parameters required to use a JavaScript method or function?
No, having a parameter for a function doesn’t mean that it will be used
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener
What is a callback function? Function passed into another function as an argument to be used later
What object is passed into an event listener callback when the event fires?
Event object - function as the call back function
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The target property returns the element that triggered the event. Check on MDN. The element where our target originated from (not attached)
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
first one is a variable, second is a function being called. Event handler is the function run when an event listener is run.
What is the className property of element objects?
It’s used to update or retrieve the class name on an element
How do you update the CSS class attribute of an element using JavaScript?
Get the element and assign the new class name property
What is the textContent property of element objects?
To add or update text within a specific element
How do you update the text within an element using JavaScript?
Using textcontent property on the DOM property and assigning it the new text content
Is the event parameter of an event listener callback always useful?
No, it’s not always necessary. The event object was not used.