DOM Flashcards
What is a “model”?
A representation or recreation of an original
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?
Refers to a JavaScript object
What is a DOM Tree?
The DOM tree is a model of a web page
Give two examples of document methods that retrieve a single element from the DOM.
- getElementById()
- querySelector() – only use querySelector
Give one example of a document method that retrieves multiple elements from the DOM at once.
- getElementsByClassName()
- getElementsByTagName()
- querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
To save the browser from needing to look up the same elements again if it is intended to be reused.
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?
So the html content can load first
What does document.querySelector() take as its argument and what does it return?
- String argument is a CSS Selector (type, class id)
- Returns the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
- String argument is a CSS Selector (type, class id)
- Returns all matching elements in a node list
How do you query for attributes?
(element + attrtype + attrname)
document.querySelector(‘p#explanation’)
What is the DOM?
Representation of a JavaScript object describing the html document
What is the purpose of events and event handling?
Enable functionality with events on the webpage
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() method
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
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
A reference to the object onto which the event was dispatched
Learn more with:
console.dir(event.target)
What is the className property of element objects?
It gets and sets class to an element
How do you update the CSS class attribute of an element using JavaScript?
$element.className = “classone classtwo“
What is the textContent property of element objects?
textContent property allows you to get and set the text inside a node
How do you update the text within an element using JavaScript?
node.textContent = “Assign text value”
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
- Stick to one source of truth, always keep data in
JS instead of the DOM
Why is storing information about a program in variables better than only storing it in the DOM?
Simplify the source of data, and prevent bugs
Does the document.createElement() method insert a new element into the page?
No
What do you pass as the arguments to the element.setAttribute() method?
- attribute type
- value
$element.setAttribute(‘id’, ‘home’)
How do you add an element as a child to another element?
element.appendChild($childElement)
What steps do you need to take in order to insert a new element into the page?
document. createElement(el)
- save into variable
query DOM for parent element that exists on HTML page
- save into variable
parentEl.appendChild(childEl)
What is the textContent property of an element object for?
textContent will get and set text to an existing node
Name two ways to set the class attribute of a DOM element.
- className
- setAttribute(‘class’ ‘value’)
- classList.add()
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
- reusability
- faster to create it once and have a loop handle it
What is the event.target?
event.target is the the location of where the event occurred
Why is it possible to listen for events on one element that actually happen its descendent elements?
An event is carried up through the event-chain due to 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?
The closest method traverses the element and it’s parent and returns closest ancestor of the selected element
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?
Add the event listener to the closest parent element that contains all the clickable DOM elements