DOM Flashcards
Why do we log things to the console?
to know what the code is doing
What is a “model”?
representation of something;
DOM tree, stored in the browser’s memory
Which “document” is being referred to in the phrase Document Object Model?
the entire page
What is the word “object” referring to in the phrase Document Object Model?
data types, collection of data
What is a DOM Tree?
all the nodes of the elements
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.
.getElementsByClassName()
Why might you want to assign the return value of a DOM query to a variable?
so the location of the value is saved and JS doesn’t have to find it again
What console method allows you to inspect the properties of a DOM element object?
.dir()
Why would a script tag need to be placed at the bottom of the HTML content instead of at the top?
HTML needs to load first because JS needs to know what to work with
What does document.querySelector() take as its argument and what does it return?
takes in a CSS selector,
returns the first match of the selector
What does document.querySelectorAll() take as its argument and what does it return?
takes in a CSS selector,
returns the all the matches of the selector in a node list
What is the purpose of events and event handling?
user interaction
What do [] square brackets mean in function and method syntax documentation?
they’re optional
What is a callback function?
a function passed into another function as an argument
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
the element that the event handler/listener is applied to
What object is passed into an event listener callback when the event fires?
event name & function ?
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
first - event is fired when clicked
second - event is fired at run time
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
first - event is fired when clicked
second - event is fired at run time
What is the className property of element objects?
list of strings with all the class names of the element changes the class name of elements
How do you update the CSS class attribute of an element using JavaScript?
.className
What is the textContent property of element objects?
can change text content in an element
How do you update the text within an element using JavaScript?
.textContent
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 complicated
Why is storing information about a program in variables better than only storing it in the DOM?
incase of future uses
Does the document.createElement() method insert a new element into the page?
no it just creates one
How do you add an element as a child to another element?
element.appendChild(elementYouWantAdded)
What do you pass as the arguments to the element.setAttribute() method?
(name, value)
What steps do you need to take in order to insert a new element into the page?
- Create element node
- Create text node
- Add text node to element node
or - Add textContent to node
- Add element to dom tree
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.
- .className
2. .setAttribute(class, value)
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
- It can create the tree with a lot of elements in one go instead of manually inputting
- reduces human error like spelling mistakes
What is the event.target?
whatever part of the document I am clicking on
Why is it possible to listen for events on one element that actually happen its descendent elements?
bubbling
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 - selectors
returns closest ancestor element
How can you remove an element from the DOM?
elementName.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?
apply event listener to the parent element