DOM(Document Object Model) Flashcards
Why do we log things to the console?
SO we can see what’s going on. Better than guessing.
What is a “model”?
Representation of something else.
Which “document” is being referred to in the phrase Document Object Model?
For this one HTML, API
What is the word “object” referring to in the phrase Document Object Model?
Each node is an object
attribute nodes or text nodes
What is a DOM Tree?
It can be thought of as a tree with a single parent stem that branches out into several child branches.
Give two examples of document methods that retrieve a single element from the DOM.
document. getElementById()
document. querySelector()
Give one example of a document method that retrieves multiple elements from the DOM at once.
document.queryselectorAll()
Why might you want to assign the return value of a DOM query to a variable?
If we need to work with an element more than once.
What console method allows you to inspect the properties of a DOM element object?
console.dir();
Why would a script 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 the JavaScript code can access them.
What does document.querySelector() take as its argument and what does it return?
takes css selector as string?
returns the first element that matches a css selector.
What does document.querySelectorAll() take as its argument and what does it return?
NodeList representing a list of the document’s elements that match the specified group of selectors.
Why do we log things to the console?
To check if it’s working.
What is the purpose of events and event handling?
to callback events??
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 function passed into another function as an argument
What object is passed into an event listener callback when the event fires?
event object?
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
with the handleclick(), it can’t do anything cuz it’s not calling function
What is the className property of element objects?
set or return the value of an element’s class attribute
How do you update the CSS class attribute of an element using JavaScript?
document.queryseletor(css selector).className = string of the class name
What is the textContent property of element objects?
sets or returns the textual content of the specified node, and all its descendants.
How do you update the text within an element using JavaScript?
elemet.textContent = ‘’
Is the event parameter of an event listener callback always useful?
Not always
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
Would say harder.
Why is storing information about a program in variables better than only storing it in the DOM?
Numbers aren’t DOM element, so we need to use a variable to assign a numeric values.
Does the document.createElement() method insert a new element into the page?
No it doesn’t
How do you add an element as a child to another element?
appendchild
What do you pass as the arguments to the element.setAttribute() method?
pass the values of the attributes and the values as string
What steps do you need to take in order to insert a new element into the page?
What is the textContent property of an element object for?
generate texts to the nodes.,
Name two ways to set the class attribute of a DOM element.
setAttribute, classname
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
To repeat
We can change it any time we want.
What is the event.target?
reference to the object onto which the event was dispatched. point of interaction
Why is it possible to listen for events on one element that actually happen its descendent elements?
The event bubbling: The event starts at the most specific node and flows outwards to the least specific one., it’s the default
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 css selector, and it returns the closest parent element. also could return iself
How can you remove an element from the DOM?
.remove() method
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?
Use bubbling