JavaScript DOM Flashcards
Why do we log things to the console?
To make sure the script is loading properly and to see what we are selecting
What is a “model”?
A “model” is the DOM tree stored in the browser’s memory which houses all the nodes of the web page: most commonly the document node, element nodes, attribute nodes, and text nodes
Which “document” is being referred to in the phrase Document Object Model?
The document node, or the HTML page
What is the word “object” referring to in the phrase Document Object Model?
It refers to the face that the model (the DOM tree) is made of objects
What is a DOM Tree?
A model of the web page that has every element, attribute, and piece of text in the HTML represented by its own DOM node
Give two examples of document methods that retrieve a single element from the DOM.
document.getElementById() and document.querySelector()
Give one example of a document method that retrieves multiple elements from the DOM at once.
document.getElementsByClassName(), document.getElementsByTagName(), and document.querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
If you need to use the same element(s) more than once, so by using a variable, you can store the location of the element(s) which saves the browser looking through the DOM tree to find the same element(s) again. This is knows as “caching” the selection
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 that the page exists and is loaded before subjecting it to DOM manipulation
What does document.querySelector() take as its argument and what does it return?
It takes a css selector as a string as its argument and returns only the first of the matching elements
What does document.querySelectorAll() take as its argument and what does it return?
It takes a css selector as a string as its argument and returns all of those that match
Why do we log things to the console?
To make sure things are as we expect them to be
What is the purpose of events and event handling?
Events specify how the user is interacting with the browser and what’s in it.
Event handling “handles” what happens when an event is triggered, usually triggering some JavaScript code, and has 3 steps: 1. selecting the element node(s) you want the script to respond to 2. indicating which event on the selected node(s) will trigger the response 3. state the code you want to run when the event occurs
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?
The event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
It is the target property of the event object. You would check it by console.log(event.target)
What is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())
When the interpreter sees the parentheses after a function call, it runs the code straight away, so you want to use the first snippet of code, unless the function needs some information (arguments) to do its job
What is the className property of element objects?
The className property of the Element interface gets and sets the value(s) of the class attribute of the specified element.
How do you update the CSS class attribute of an element using JavaScript?
.className =