dom Flashcards
Why do we log things to the console?
access of value
What is a “model”?
representation of a structure
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?
js object; a object that is modeling the object document
What is a DOM Tree?
object model; model made of objects; stored in browser’s memory
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()
Why might you want to assign the return value of a DOM query to a variable?
reuse the method
What console method allows you to inspect the properties of a DOM element object?
method dir of object console; console.dir()
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
gives the HTML time to load before any of the JavaScript loads, which can prevent errors
What does document.querySelector() take as its argument and what does it return?
arg: a string css selector
return: first of the matching elements from top
What does document.querySelectorAll() take as its argument and what does it return?
arg: a string css selector
return: node list of one element object for each element that matches the css selector
dom node?
an object with methods and properties; every node is a descendant of the document node
Why do we log things to the console?
.
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener()
Are all possible parameters required to use a JavaScript method or function?
no
What object is passed into an event listener callback when the event fires?
the event object
What is a callback function?
a function passed into another function as an argument
What object is passed into an event listener callback when the event fires?
the event object; just a report on that event
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 at the event object; a reference to the object onto which the event was dispatched; mdn
What is the className property of element objects?
retrieve/assign the value of an element’s class attribute
How do you update the CSS class attribute of an element using JavaScript?
query and assign a new variable to the className property
What is the textContent property of element objects?
add or update text w/in a dom element
How do you update the text within an element using JavaScript?
use the textContent property to the element
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?
complicated
Why is storing information about a program in variables better than only storing it in the DOM?
data accessibility
Why is storing information about a program in variables better than only storing it in the DOM?
data accessibility; keep it w/in js rather than dom
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?
append child method
What do you pass as the arguments to the element.setAttribute() method?
name of attribute and it’s respective object property
What steps do you need to take in order to insert a new element into the page?
use a create element method of the document object
What is the textContent property of an element object for?
the text content of the object and it’s children
Name two ways to set the class attribute of a DOM element.
set attribute method and class name method
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
data respective
What is the event.target?
getting the target of the event (object; most specific element interacted with); reference to the object onto which the event occurred
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling: the event starts at the most specific node and flows outwards to the least specific one; the default type of event flow
What DOM element property tells you what type of element it is?
tagName; which is a read-only property of the element interface returns the tag name of the element on which it’s called
What does the element.closest() method take as its argument and what does it return?
arg: a selector list
return: element which is the closest ancestor of the selected element
How can you remove an element from the DOM?
Element.remove() method removes the element from the tree it belongs to
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?
Event delegation
what is event delegation?
Event delegation allows you to avoid adding event listeners to specific nodes; instead, the event listener is added to one parent
What is the event.target?
reference to the object onto which the event occurred
What is the affect of setting an element to display: none?
will remove it from the accessibility tree
What does the element.matches() method take as an argument and what does it return?
arg: string representing the selector
return: boolean value
How can you retrieve the value of an element’s attribute?
get attribute method
At what steps of the solution would it be helpful to log things to the console?
each step continually
If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?
event capturing; add another event listeners to new tab node; least specific node to most specific
If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?
check each attribute property of each element in separate conditionals