DOM Flashcards
Why do we log things to the console?
to see what our code is doing
What is a “model”?
a representation of something else
Which “document” is being referred to in the phrase Document Object Model?
the HTML document
What is the word “object” referring to in the phrase Document Object Model?
referring to the JavaScript objects that the DOM reads the document as
What is a DOM Tree?
all the nodes of an element; a model of a web page the browser stores in memory (DOM element plus all the children inside 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.
getElementByClassName( )
getElementByTagName( )
querySelectorAll( )
Why might you want to assign the return value of a DOM query to a variable?
if you want to work with that element node more than once
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 the elements of the HTML page before the JavaScript code can access them
What does document.querySelector() take as its argument and what does it return?
argument: a string containing a CSS selector describing the element you want to work with
return: ONE HTMLElement object representing the first element in the document that matches the selector
What does document.querySelectorAll() take as its argument and what does it return?
argument: DOMString containing one or more selectors
return: a NodeList containing one Element object for each element that matches at least one of the specified selectors, otherwise an empty NodeList if none match
Why do we log things to the console?
to see how our code is working
What is the purpose of events and event handling?
allows your page to be interactive
What do [ ] square brackets mean in function and method syntax documentation?
it means it is optional to include
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 function that gets passed into another function as an argument which is called within the outer function to do some action
- a function being passed as data
What object is passed into an event listener callback when the event fires?
the event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
An HTML element that triggered this event
you can console log it and check mdn for more information
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
the first one runs the handleClick function when the event is fired
the second one indicates that the handleClick function should run as the page loads
What is the className property of element objects?
string value containing each class for the element separated by spaces
How do you update the CSS class attribute of an element using JavaScript?
elm.setAttribute(‘class’, elm.getAttribute(‘class’))
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?
use textContent property on the element object and assign it a new value
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?
more difficult
Why is storing information about a program in variables better than only storing it in the DOM?
allows to reuse more than once simply and efficiently, easier to read
What does the transform property do?
lets you rotate, scale, skew or translate an element
Give four examples of CSS transform functions.
matrix, translate, rotate, skew, scale
What is the event.target?
target property of event interface, reference to element where the event originated from
Why is it possible to listen for events on one element that actually happen its descendent elements?
bubbling (elaborate)
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?
argument: selector
return: closest ancestor
How can you remove an element from the DOM?
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?
dom event delegation
putting an event listener on the element above the element you want it to originate on