DOM Flashcards
What is a “model”?
A template to follow
Which “document” is being referred to in the phrase Document Object Model
the current html document
What is the word “object” referring to in the phrase Document Object Model?
The elements of the document are represented as objects
What is a DOM tree?
A visual representation of all of the document’s nodes and their relation to one another
Give two examples of document methods that retrieve a single element from the DOM.
document. querySelector(‘css selector’)
document. getElementById(‘id’)
Give one example of a document method that retrieves multiple elements from the DOM at once.
document. querySelectorAll( )
document. getElementsByClassName( )
document. getElementsByTagName( )
Why might you want to assign the return value of a DOM query to a variable?
The variable does not hold the value of the node itself, but a reference to where that node is stored. So, if you want to do multiple things to that node, it is useful to store it in a variable so that the computer does not have to search for it every time
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 through the entire HTML body
What does document.querySelector( ) take as its argument and what does it return?
It takes in a CSS selector and returns the first element that matches the result
What does document.querySelectorAll( ) take as its argument and what does it return?
CSS selector as argument
Returns a NodeList of all elements that match
What is the purpose of events and event handling?
Makes the page more interactive by triggering scripts when certain events happen
What method of element objects lets you set up a function to be called when a specific type of event occurs?
.addEventListener(event, callback function)
What is a callback function?
A function that is passed as an argument into another function.
A function that you don’t want to call right away so you store for later
What object is passed into an event listener callback when the event fires?
the event object
What is the event.target? If you’re not sure, where would you find information about it?
property that returns the object which the event was bounded onto
Difference between:
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The first one is a callback function; Only runs when the event occurs
Second one is a normal function call and it runs as the web page is loaded only.
What is the className property of element objects?
returns a string of the value of the class attribute for the element
How do you update the CSS class attribute of an element using JavaScript?
update the className property of the element
What is the textContent property of element objects?
Gets the text content of the element as well as the text content of its children(?)
How do you update the text within an element using JavaScript?
with the .textContent property of an element
Is the event parameter of an event listener callback always useful?
No, but always present. Do not forget event parameter when defining event handler functions
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
More difficult as the click counter allows us to control both the text content and the button color
Why is storing information about a program in variables better than only storing it in the DOM?
It is easier to reference especially if using multiple times