THE DOM Flashcards
Why do we log things to the console?
To see a variable’s contents, to see what an element contains when using dom query commands.
What is a “model”?
It is a representation of something else.
Which “document” is being referred to in the phrase Document Object Model?
The HTML document
What is the word “object” referring to in the phrase Document Object Model?
The model is made of objects, each object in the model represents a different part of the page in the browser window.
What is a DOM Tree?
A DOM element and all of its DOM child elements and contents.
Give two examples of document methods that retrieve a single element from the DOM.
document.querySelector(), document.getElementById()
Give one example of a document method that retrieves multiple elements from the DOM at once.
document.getElementByClassName(), document.getElementByTagName(), document.querySelectorAll().
Why might you want to assign the return value of a DOM query to a variable?
So you can keep track of an element Node’s location in order to use it multiple times without retrieving it again.
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?
The browser needs to parse all of the elements before interacting with them
What does document.querySelector() take as its argument and what does it return?
A css selector (tag name, class name, id name) it returns the location of the first of that matching element
What does document.querySelectorAll() take as its argument and what does it return?
Css selector (tag name, class name, id name) and returns all of the elements that match in a node list
What is the purpose of events and event handling?
Do react and trigger events when something is done by the user
What do [] square brackets mean in function and method syntax documentation?
They are optional
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?
It is a function passed into another function as an argument
What object is passed into an event listener callback when the event fires?
An event object with all of the info of that event.
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 dom element that triggered the event. You can log the event and see the target element in the target property
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
With out parenthesis means that the function should be called when the event fires, with () means that that function should run as the page loads
What is the className property of element objects?
It is a method that can be used to grab the class names of a dom element as a string, or be reassigned
How do you update the CSS class attribute of an element using JavaScript?
domelement.className = ‘className’. This rewrites the entire class attribute so if you are adding one class you need to re-add the other classes
What is the textContent property of element objects?
It is a method that can be used to grab the text content of a dom element as a string, or be reassigned
How do you update the text within an element using JavaScript?
With .textContent
Is the event parameter of an event listener callback always useful?
no
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 retrieve it from the dom each time
Why is storing information about a program in variables better than only storing it in the DOM?
It is important because it is similar for the computer to access a variable with the value than pulling it from the dom each time.
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?
Using the .appendChild method
What do you pass as the arguments to the element.setAttribute() method?
(‘attribute type’, ‘attribute value’)
What steps do you need to take in order to insert a new element into the page?
Create element node, create text node (if needed), set attributes (if needed), add text nodes and attributes, add to dom tree.
What is the textContent property of an element object for?
To add text content to an element
Name two ways to set the class attribute of a DOM element.
.setAttribute, .className, classList
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
You can replicate the same dom tree section and give it different values.
What is the event.target?
The target is the element that the event was triggered from
Why is it possible to listen for events on one element that actually happen its descendent elements?
Because event flow bubbling
What DOM element property tells you what type of element it is?
event.target.tagName
What does the element.closest() method take as its argument and what does it return?
It takes a selector in string form and returns the element that is closest to the selector
How can you remove an element from the DOM?
element.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?
You would add an event listener to the parent of the elements being added.