DOM Flashcards
Why do we log things to the console?
To be able to see how the page interacts with what we’ve written
Debugging and analysis
What is a “model”?
It’s a recreation that’s an essence (but not 1:1 ratio) stored in the computer’s memory of how the website is structured in relation to the document and elements on the page
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 javascript objects
What is a DOM Tree?
Document Object Model
Recreation of the javascript object modeling the HTML content loaded in the browser
DOM tree can be the whole page, but it can also be a smaller section, where there’s a parent, child, descendent relation, however large or small
It’s a tree because of the relationships between the document and all the elements nodes are described like a family tree: parents, children, siblings, ancestors and descendents.
Give two examples of document methods that retrieve a single element from the DOM.
querySelector(), getElementById()
**querySelector() is basically the one mostly used
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll(), getElementsByClassName(), GetElementsByTagName()
**querySelectorAll() is also the one mostly used
Why might you want to assign the return value of a DOM query to a variable?
Permanent reference to that one element saves the computer time to not query/look for it everytime
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?
HTML loads top to bottom,
All the information needs to be loaded and in place before an action can happen via javascript
What does document.querySelector() take as its argument and what does it return?
Any CSS selector, written as string
Returns the first element object that matches the CSS selector
What does document.querySelectorAll() take as its argument and what does it return?
Any CSS selector, written as string
Returns all element objects that matches the CSS selector in the form of a Node List
Node list is object created to hold a list of dom elements
No ability to listen to event listeners, just DATA
But you can adjust every element IN the node list with a for loop, using the length of the node list
What is the purpose of events and event handling?
This is to make the user interaction with the webpage more organic and dynamic
Event handling is for something to happen when the event occurs
Are all possible parameters required to use a JavaScript method or function?
No, often it’s optional, like the eventListener has a 3 argument that is optional with default useCapture:false, so you don’t need to write it
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’s a function passed into another function as an argument, which is invoked inside the outer function to complete some kind of routine or action
For example, event listeners will call the other function it’s trying to pass, but only javascript will call it when the event happens
What object is passed into an event listener callback when the event fires?
The event object
Contents returned are the data of what happened, where, how, when, etc
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
This is the targeting property of the event that is happening.
It’s the element where the interaction of the event occurred, NOT where the listener was applied
For example, it’ll tell you the property of where the event is that you’re calling or trying to modify
What is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())
The first one will run a variable named handleClick which will run a function named handleClick upon the event ‘click’
The second one will call the handleClick() function immediately instead of after an event with undefined as an argument which will give back undefined and do nothing and thus render the ‘click’ function useless/bugged.
What is the className property of element objects?
className can get or set the value of the property of the HTML element
How do you update the CSS class attribute of an element using JavaScript?
object.className = ‘new class’
What is the textContent property of element objects?
It’s the text content of the node and its descendents
Text content that is present in the element