DOM Flashcards
Why do we log things to the console?
Powerful tool for debugging and an easy way to inspect your variables in the browser
What is a “model”?
A 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?
A JavaScript object modeling the HTML document’s content
What is a DOM Tree?
Tree structure of nested nodes (element, text, comment), like a family tree that shows the relationship between parents, children, and siblings.
Give two examples ofdocumentmethods that retrieve a single element from the DOM.
querySelector() - uses a CSS selector, and returns the first matching element
getElementById() - uses the value of an element’s id attribute (which should be unique within the page)
**querySelector() only one needed, consistency
Give one example of adocumentmethod that retrieves multiple elements from the DOM at once.
querySelectorAll() - uses a CSS selector to select all matching elements
getElementsByClassName() - selects all elements that have a specific value for their class attribute getElementsByTagName() - selects all elements that have the specified tag name
Why might you want to assign the return value of a DOM query to a variable?
Caching the selection - If your script needs to use the same element(s) more than once, you can store the location of the element(s) in a variable
Storing the location of the element(s) within the DOM tree in a variable
Whatconsolemethod allows you to inspect the properties of a DOM element object?
console.dir() - displays an interactive list of the properties of the specified JavaScript object, the output is presented as a hierarchical listing with disclosure triangles that let you see the contents of the child objects
Why would atag 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 in the HTML page before the JavaScript code can access them
What doesdocument.querySelector()take as its argument and what does it return?
A string containing a CSS selector syntax that would select one or more elements
Returns only the first of the matching elements
What doesdocument.querySelectorAll()take as its argument and what does it return?
A string containing CSS selector syntax to select one or more elements
Returns all of those that match in a NodeList (array-like object)
What is the purpose of events and event handling?
Events -an action that occurs as a result of the user or another source, like a mouse click
Event handing - a routine that deals with the event, allowing a programmer to write code that is executed 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(type, listener, options);
addEventListener(type, listener, useCapture);