DOM Flashcards
Why do we log things to the console?
To check that our code is doing what we intended for it to do
What is a “model”?
Representation of how the html page is structured
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?
Datatype object (in JavaScript)
What is a DOM Tree?
Collection of html elements (like a family tree) AND all of its child elements as well as any relevant information attached to them
Give two examples of document methods that retrieve a single element from the DOM.
o getElementbyID(‘id’) o querySelector(‘css selector’)
Give one example of a document method that retrieves multiple elements from the DOM at once.
o getElementsByClassName(‘class’) o getElementsByTagName(‘TagName’) o querySelectorAll(‘css selector’)
Why might you want to assign the return value of a DOM query to a variable?
To allow browser to locate the element faster in the DOM (if you’re going to be using this return value 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?
Because the browser needs to read all the elements in the HTML page before JavaScript can access them
What does document.querySelector() take as its argument and what does it return?
It takes a STRING containing a CSS selector and returns the first DOM element object with that selector.
What does document.querySelectorAll() take as its argument and what does it return?
It takes a STRING containing a CSS selector and gives a NodeList of elements.
What is the purpose of events and event handling?
To make webpages interactive
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?
Function that you pass inside another function as an argument
What object is passed into an event listener callback when the event fires?
Event object (object passed by JS that has all the information about the event that occured)
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
A read-only property that shows which object(or html element) triggered the event
Check through the console log and see which element gets logged
What is the difference between these two snippets of code?
- element.addEventListener(‘click’, handleClick)
- element.addEventListener(‘click’, handleClick())
1 has a callback function
2 has the return of the function call
What is the className property of element objects?
Sets and returns the class name of a specified element
How do you update the CSS class attribute of an element using JavaScript?
By using element.className = ‘new-class-name-in-html-format’