DOM Flashcards
Why do we log things to the console?
It is a way to debug scripts and make sure scripts are running smoothly
So we can look at what we are working with
What is a “model”?
It is an imitation of something else
Which “document” is being referred to in the phrase Document Object Model?
HTML code
What is the word “object” referring to in the phrase Document Object Model?
Data type object
What is a DOM Tree?
A parent element and all of its child elements
As a browser loads a web page, it creates a model of that page. The model is called a DOM tree, and it is stored in the browsers’ memory.
It consists of 4 types of nodes: Document node Element node Attribute node Text Nodes
Give two examples of document methods that retrieve a single element from the DOM.
getElementById()
querySelector() - uses a CSS selector, and returns the first matching object - only one we need
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementByClassName()
getElementByTagName()
querySelectorAll() -only one to use
Why might you want to assign the return value of a DOM query to a variable?
It is easier to re-use that element
Saves the browser looking through the DOM tree to find the same elements again. It is known as caching the selection
What console method allows you to inspect the properties of a DOM element object?
console.dir(object)
Why would a 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?
returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.
What does document.querySelectorAll() take as its argument and what does it return?
Returns the node list of all elements
Node list is not an array
Why do we log things to the console?
It is a way to debug scripts and make sure scripts are running smoothly
So we can look at what we are working with
What is the purpose of events and event handling?
Allows users to interact with the page
Our way to respond to things happening
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 callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
What object is passed into an event listener callback when the event fires?
Event object with all the info about the event that occurred
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
Property of the event object
Points to the element where the event occured
Checking mdn
What is the className property of element objects?
The className property of the Element interface gets and sets the value of the class attribute of the specified element.
How do you update the CSS class attribute of an element using JavaScript?
First get the element using a method from the document object then you use the class name object and assign a new string to it
What is the textContent property of element objects?
The textContent property of the Node interface represents the text content of the node and its descendants.