DOM Flashcards
why do we log things to the console?
to examine if the browser is showing the right value
what is a “model”?
representation of the objects (replica)
which “document” is being referred to in the phrase Document Object Model?
web page that can be displayed in the browser window or as the html source.
what is the word “object” referring to in the phrase Document Object Model?
- data type object in javascript
- DOM is javascript object which is a model of the html document
- it is not your html document. it is a response for your html document
what is a DOM Tree?
The DOM represents a document with a logical tree. Each branch of the tree ends in a node, and each node contains objects.
parent elements containing child elements with nodes.
Give two examples of document methods that retrieve a single element from the DOM.
getElementById,
querySelector (only use this) - for consistency
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll
why might you want to assign the return value of a DOM query to a variable?
store the info that you need and access to it again
what console method allows you to inspect the properties of a DOM element object?
dir
why would a tag need to be placed at the bottom of the html content instead of at the top?
to load the html and css first, and then javascript to be loaded
what does document.querySelector() take as its argument and what does it return?
string of the css selector and returns the first one of the matching elements.
what does document.querySelectorAll() take as its argument and what does it run?
css selector and returns a NodeList (collection of data that matches the element and can’t add items)
what is the purpose of events and event handling? -> interactive web apps
Events are actions or occurrences that happen in the system you are programming, which the system tells you about so your code can react to them.
To react to an event, you attach an event handler to it. This is a block of code (usually a JavaScript function that you as a programmer create) that runs when the event fires.
When such a block of code is defined to run in response to an event, we say we are registering an event handler
Are all possible parameters required to use a JavaScript method or function?
no. can call a function without a parameter.
what method of element objects lets you set up a function to be called when a specific type of event occurs?
addEvenListener
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?
object with all data of whatever event occurred.
what is the ‘event.target’?
points to the element that the event occurred.
- What is the difference between these two snippets of code?
1) element.addEventListener(‘click’, handleClick)
2) element.addEventListener(‘click’, handleClick())
the second one doesn’t access the object?
- uncaught typeerror: cannot read properties of undefined (reading ‘target’)
the parentheses are omitted where the function is called because they would indicate that the function should run as the page loads (rather than when the event fires)
what is the ‘className’ property of element objects?
the className property of the Element interface gets and sets the value of the class attribute of the specified element.
how do you update the css class attribute of an element using Javascript?
get the method from the object and add the className property
what is the textContent property of element objects?
the textContent property of the Node interface represents the text content of the node and its descendants
how do you update the text within an element using Javascript?
assigning to the textContent property.
Is the ‘event’ parameter of an event listener callback always useful?
no. sometimes it’s not used inside of the function. but still put the ‘event’ parameter.
=> clear indicator that we’re using the eventHandler. never call that function itself.