DOM (Document Object Model) Flashcards
Why do we log things to the console?
Inspect value to debug - This is especially important for DOM
What is a “model”? (Not in relation to code)
A prototype or representation of something
Which “document” is being referred to in the phrase Document Object Model?
HTML Document
What is the word “object” referring to in the phrase Document Object Model?
Objects in JavaScript
What is a DOM Tree?
JavaScript object for a HTML element including attributes, values, and its children elements
Give two examples of document methods that retrieve a single element from the DOM.
getElementByID
querySelector (preferred method)
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll (preferred method)
Why might you want to assign the return value of a DOM query to a variable?
Easier access so you can search for it and use it again in the future
What console method allows you to inspect the properties of a DOM element object?
console.dir (which stands for directory)
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
HTML content needs to load the data before using DOM or else it will have nothing to work with
What does document.querySelector() take as its argument and what does it return?
String that consists of a CSS selector and returns the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
String that consists of a CSS selector and returns all those that match (node list)
What is the purpose of events and event handling?
Creating a reaction in response to something happening.
Are all possible parameters required to use a JavaScript method or function?
No some are optional
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 within a function that is being passed as another argument
What object is passed into an event listener callback when the event fires?
Object that contains the data of that event
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it
Where the event occurred.
You can check in the console.log
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The first will run when the event fires.
The second will return the value which would be undefined. We will never call the event listener, the browser technically does this which is why the second code snippet is wrong.
What is the className property of element objects?
Property stored on the DOM element object which stores the class attribute of that element
How do you update the CSS class attribute of an element using JavaScript?
className property with a value assignment
What is the textContent property of element objects?
Allows us to retrieve or manipulate text content
How do you update the text within an element using JavaScript?
textContent property with a value assignment
Is the event parameter of an event listener callback always useful?
Not always - all the information isn’t always necessary