DOM Flashcards
Why do we log things to the console?
To be aware of what our code Is doing and to make sure it has the outcome we want
What is a “model”?
A replication of a dom tree that is stored in the browser’s terminal
Which “document” is being referred to in the phrase Document Object Model?
the HTML document we are working on
What is the word “object” referring to in the phrase Document Object Model?
Each object refers to a different part of the page in the browser
What is a DOM Tree?
Collection of all the objects that make up the document
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 you know that you need to call on that query multiple times
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?
So it can run the entire body to map out a tree before running our script
What does document.querySelector() take as its argument and what does it return?
Takes our class or id as an argument and returns the first instance of it
What does document.querySelectorAll() take as its argument and what does it return?
Takes all the elements or class we are looking for and returns a nodes list
What are some differences of Nodes list and HTML collection?
o Html collection will live update as you add things
o Nodes list is static and would not update
What is the purpose of events and event handling?
Events will trigger code. Event handling is to run blocks of code based on what happens on our webpage
What method of element objects let 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 which is then invoked inside the outer function to complete some kind of action
What object is passed into an event listener callback when the event fires?
The event object. It gives us information about the event that actually occurs
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The specific html element that was interacted with and that caused it to occur
What is the difference between these two snippets of code? element.addEventListener(‘click, handleClick) & element.addEventListener(‘click’, handleClick())
a function without the parenthesis is a reference to the function. With parenthesis you are actually calling it.
What is the className property of element objects?
the current classes on the object and allows us to dynamically update it
How do you update the CSS class attribute of an element using Javascript?
By using .className = and then the name you want to change it to
What is the textContent property of element objects?
It represents the text content of the node and its descendants
How do you update the text within an element using JavaScript?
Calling that element.textContent = the new value you want to assign to that element
Is the event parameter of an event listener callback always useful?
It is useful but sometimes you do not need it, sometimes it is useful to know what the function is expecting in its parameter though
Is the event parameter of an event listener callback always useful?
It is useful but sometimes you do not need it, sometimes it is useful to know what the function is expecting in its parameter though
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 because we would have to query the dom everytime we needed to use an element. Instead we can just store the results so it does not have to query again
Why is storing information about a program in variables better than only storing it in the DOM?
Because the variable will always be able to find where that information is without having to go through the whole html file first
The dom will be dynamic and constantly be changing things
What happens if you do not give .className a value?
it will just tell you what the class is doing
Why is innerHTML bad?
Puts exposure to hacking
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?
appendChild() method
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 it and then append it to something
What is the textContent property of an element object for?
You can get textContent and use it to set textContent
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)?
Main advantage is that it is reusable
What is the event.target?
o it is a reference to the object which the event is occuring to
o whatever element that was actually interacted with
Why is it possible to listen for events on one element that actually happen its descendent elements?
Happens on the child and bubbles upward
What DOM element property tells you what type of element it is?
o .tagName
o It will tell you which element you are actually interacting with if you use a ‘click’ event
What does the element.closest() method take as its argument and what does it return?
o This method will go from the current element to whichever id, class, or element you put in as an argument
o Css selector and will return the first one that matches it
How can you remove an element from the DOM?
Item you want to remove .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?
Add the event listener to the parent so that you will capture everything that happens to its children