DOM Flashcards
(40 cards)
Why do we log things to the console?
To make sure our code works
What is a “model”?
A representation of an object that is typically smaller than the object that is being modeled
Which “document” is being referred to in the phrase Document Object Model?
The html document being loaded into a browser
What is the word “object” referring to in the phrase Document Object Model?
Each individual elements on the html page (including the document itself)
What is a DOM Tree?
The drawn mapping of the elements of the html page
Give two examples of document methods that retrieve a single element from the DOM.
querySelector(‘css selector’)
getElementById(‘id’)
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll(‘css selector’)
getElementsByClassName(‘class’)
Why might you want to assign the return value of a DOM query to a variable?
Why might you want to assign the return value of a DOM query to a variable?
What console method allows you to inspect the properties of a DOM element object?
console.dir(variableName)
Why would a 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 in the HTML page before the JavaScript code can access them.
What does document.querySelector() take as its argument and what does it return?
It takes any valid CSS selector as its argument
Returns the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
It takes any valid CSS selector as its argument
Returns all matching elements in a node list
Why do we log things to the console?
To make sure our code works
What is the purpose of events and event handling?
Events are any changes to a document whether it be from user interaction or changes from within the browser.
Event handlers trigger functions depending on the event
What do [] square brackets mean in function and method syntax documentation?
They represent optional arguments
What method of element objects lets you set up a function to be called when a specific type of event occurs?
element.addEventListener(‘event’, callbackFunction);
What is a callback function?
A function that is passed as an argument in another function’s call
What object is passed into an event listener callback when the event fires?
The event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
It’s full html element being targeted by a dom query
You could check by logging onto the console or check mdn documentation
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The first one takes a callback function as the argument and the second one runs the function and whatever that is returned will be used as the argument for the addEventListener method
What is the className property of element objects?
It accesses the class attribute of an html element
How do you update the CSS class attribute of an element using JavaScript?
Query the dom for an element then use the className property to assign a string value to update the class name
What is the textContent property of element objects?
It accesses all text within the opening and closing tags of an element
How do you update the text within an element using JavaScript?
Query the dom for an element then use the textContent property to assign a string value to update the text content