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.
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
It would be more complicated
Why is storing information about a program in variables better than only storing it in the DOM?
We don’t want to have to look elsewhere for the information. Keep the JavaScript data in JavaScript.
Does the document.createElement() method insert a new element into the page?
No
How do you add an element as a child to another element?
anotherElement.appendChild(element)
What do you pass as the arguments to the element.setAttribute() method?
(‘attribute name’, ‘value’)
What steps do you need to take in order to insert a new element into the page?
Create new element stored in a variable (createElement), create new text stored in a variable (createTextNode), attach text node to element with appendChild, find the position where the new element stored in a variable, insert new element into its position with appendChild().
What is the textContent property of an element object for?
Setting or returning the text content of specified node and all its descendants
Name two ways to set the class attribute of a DOM element.
className or setAttribute
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
Use it in a addEventListener, use the function over again
What is the event.target?
The target property returns the element that triggered the event
Why is it possible to listen for events on one element that actually happen on its descendent elements?
Because of event bubbling. Event is being carried forward to each ancestor of the chain
What DOM element property tells you what type of element it is?
tagName
What does the element.closest() method take as its argument and what does it return?
It takes a string CSS selector and returns closest ancestor of it
How can you remove an element from the DOM?
elementToRemove.remove()
If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?
By delegating the event to the parent element that contains all the clickable elements