DOMS Flashcards
Why do we log things to the console?
To debug and verify
What is a “model”?
A representation of something.
Which “document” is being referred to in the phrase Document Object Model?
It refers to the fact that each node is an object.
Node (for our purposes most likely refers to an HTML element)
What is a DOM Tree?
An element and all of its content
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?
You might need to work with an element multiple times, so the computer doesn’t have to constantly look for that reference to the variable.
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 the top?
Because if you put it on the top, the html code isn’t loaded yet, and you can’t access the elements.
What does document.querySelector() take as its argument and what does it return?
CSS selector
returns the JS object
What does document.querySelectorAll() take as its argument and what does it return?
CSS selector
returns a node list
Why do we log things to the console?
Debugging and verification
What is the purpose of events and event handling?
When you want to do something when something else occurs.
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?
It is a function used as an argument in another function.
What object is passed into an event listener callback when the event fires?
It is the object of the event that was used
e.g. function handleDoubleClick(event) {}
You don’t define the event parameter yourself
- JavaScript prepacks it with all the information about the type of event handleDoubleClick is used with addEventListener
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The event target is a reference to the object that caused the event to happen.
use the console and look at MDN
The target is where the event directly occurred from.
What is the difference between these two snippets of code?
o element.addEventListener(‘click’, handleClick)
o element.addEventListener(‘click’, handleClick())
o element.addEventListener(‘click’, handleClick)
- runs when the event happens
o element.addEventListener(‘click’, handleClick())
- runs when the page loads
What is the className property of element objects?
It is the value of the class attribute.
How do you update the CSS class attribute of an element using JavaScript?
variableName.className = “new class”
variableName.classList.add(‘newClass’)
variableName.classList.remove(‘removeThisClass’)