DOM (Document Object Model) Flashcards
Why do we log things to the console?
Inspect value to debug - This is especially important for DOM
What is a “model”? (Not in relation to code)
A prototype or representation of something
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?
Objects in JavaScript
What is a DOM Tree?
JavaScript object for a HTML element including attributes, values, and its children elements
Give two examples of document methods that retrieve a single element from the DOM.
getElementByID
querySelector (preferred method)
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll (preferred method)
Why might you want to assign the return value of a DOM query to a variable?
Easier access so you can search for it and use it again in the future
What console method allows you to inspect the properties of a DOM element object?
console.dir (which stands for directory)
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
HTML content needs to load the data before using DOM or else it will have nothing to work with
What does document.querySelector() take as its argument and what does it return?
String that consists of a CSS selector and returns the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
String that consists of a CSS selector and returns all those that match (node list)
What is the purpose of events and event handling?
Creating a reaction in response to something happening.
Are all possible parameters required to use a JavaScript method or function?
No some 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?
Function within a function that is being passed as another argument
What object is passed into an event listener callback when the event fires?
Object that contains the data 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
Where the event occurred.
You can check in the console.log
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The first will run when the event fires.
The second will return the value which would be undefined. We will never call the event listener, the browser technically does this which is why the second code snippet is wrong.
What is the className property of element objects?
Property stored on the DOM element object which stores the class attribute of that element
How do you update the CSS class attribute of an element using JavaScript?
className property with a value assignment
What is the textContent property of element objects?
Allows us to retrieve or manipulate text content
How do you update the text within an element using JavaScript?
textContent property with a value assignment
Is the event parameter of an event listener callback always useful?
Not always - all the information isn’t always necessary
Why is storing information about a program in variables better than only storing it in the DOM?
You can reuse it later and you don’t want to intermingle your languages in a way where they become dependent on each other.
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?
appendChild method
What do you pass as the arguments to the element.setAttribute() method?
String name and then the value
What steps do you need to take in order to insert a new element into the page?
- createElement
- Store the new element
- Query for parent element
- Call appendChild on parent with new element as an argument
What is the textContent property of an element object for?
Stores textContent of the HTML element to retrieve or change
Name two ways to set the class attribute of a DOM element.
className
setAttribute
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
Reusable and its easier to debug and test for it’s intended purpose.
What is the event.target?
Element where the event occurs
Why is it possible to listen for events on one element that actually happen its descendent elements?
Bubbling
What DOM element property tells you what type of element it is?
tagName
What does the element.closest() method take as its argument and what does it return?
String of CSS selector and returns closest ancestor element that matches pattern
How can you remove an element from the DOM?
remove method
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 an eventListener to parent element and then run a condition to see if it is targeting an element