DOM Flashcards
Why do we log things to the console?
For debugging and an easy way to inspect your variables in the browser
What is a “model”?
Replica
Which “document” is being referred to in the phrase Document Object Model?
The HTML
What is the word “object” referring to in the phrase Document Object Model?
JavaScript object
What is a DOM Tree?
DOM element and all of the items in it
Give two examples of document methods that retrieve a single element from the DOM
getElementById()
querySelector()
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
For reusability and accessibility
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 run the html document first in order to create a DOM
What does document.querySelector() take as its argument and what does it return?
CSS selector and it returns the first element from the document
What does document.querySelectorAll() take as its argument and what does it return?
CSS selector and it returns NodeList of all elements matching the selector
What is the purpose of events and event handling?
Events are actions or occurrences that happen in the system
Event handling is steps to trigger some JavaScript code
Are all possible parameters required to use a JavaScript method or function?
No
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?
A callback function is a function passed into another function as an argument
What object is passed into an event listener callback when the event fires?
An object with all of the possible data the browser can provide
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 of the Event interface is a reference to the object onto which the event was dispatched
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
In the first snippet, the event listener will call on the handleClick function only when the event occurs
In the second snippet, the event listener is calling on the handleClick function but that immediately calls the function
What is the className property of element objects?
Gets and sets the value of the class attribute of the specified element.
How do you update the CSS class attribute of an element using JavaScript?
Assign a string with new property name to the className property of element object
What is the textContent property of element objects?
Represents the text content of the node and its descendants.
How do you update the text within an element using JavaScript?
Assign a string with new text to the textContent property of element object
Is the event parameter of an event listener callback always useful?
No
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
No, we would need the variable to keep track of number of clicks or we would have to make a new variable every time
Why is storing information about a program in variables better than only storing it in the DOM?
For easy reusability and accessibility
Does the document.createElement() method insert a new element into the page?
It does not
How do you add an element as a child to another element?
element.appendChild()
What do you pass as the arguments to the element.setAttribute() method?
The name and the value of the attribute
What steps do you need to take in order to insert a new element into the page?
Create the element, give it content, add it to the dom
What is the textContent property of an element object for?
Represents the text content of the node and its descendants.
Name two ways to set the class attribute of a DOM element.
element. setAttribute()
element. className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
Saves time in the long rung
Easy to reuse
What is the event.target?
The target of the event (most specific element interacted with)
Why is it possible to listen for events on one element that actually happen its descendent elements?
Because of event bubbling
When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.
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?
Takes a DOMString containing a selector list
Returns the element which is the closest ancestor of the selected element
How can you remove an element from the DOM?
Element.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?
Add the DOM element inside the parent node containing the other DOM elements