DOM Flashcards
Why do we log things to the console?
We log things as a console both to make sure that the main.js file is interacting with the html file and so that we as developers can test and track values (debugging)
What is a “model”?
A recreation of something (generally not 1:1 recreation)
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 object data type
What is the DOM?
A JavaScript object that is modeling the contents of an HTML document that is loaded into the browser
What is a DOM Tree?
The DOM tree is a list of objects/nodes that branch off of the document node. It is made up of the different elements, attributes, and text nodes in an HTML document. It is an element plus all of its content and all of its configurations.
Give two examples of document methods that retrieve a single element from the DOM.
document. getElementbyID(‘id’);
document. querySelector(‘css selector’);
Give one example of a document method that retrieves multiple elements from the DOM at once.
document. querySelectorAll(‘css selector’);
document. getElementsByClassName(‘class’);
document. getElementsByTagName(‘tag name’);
Why might you want to assign the return value of a DOM query to a variable?
In case you need to re-use that value of a DOM query multiple times
What console method allows you to inspect the properties of a DOM element object?
console.dir( ); [ dir is short for directory]
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
Because we want to load the content of the HTML document first?
What does document.querySelector() take as its argument and what does it return?
document.querySelector( ); takes a css selector as it’s argument (in a string) and returns the first element that matches the selector
What does document.querySelectorAll() take as its argument and what does it return?
document.querySelectorAll( ); takes a css selector as it’s argument (in a string) and returns all elements with that css selector (Node List)
Why do we log things to the console?
We log things as a console both to make sure that the main.js file is interacting with the html file and so that we as developers can test and track values (debugging)
What is the purpose of events and event handling?
Events are a way to let us know when an event occurs (usually when a user interacts with the page) and event handling is our way of preparing a response when the event occurs
Are all possible parameters required to use a JavaScript method or function?
No, there are some parameters that are optional
What method of element objects lets you set up a function to be called when a specific type of event occurs?
Use addEventListener to the object you want to call the event on
What is a callback function?
A callback function is a function benign passed into another function as an argument
What object is passed into an event listener callback when the event fires?
The event object is passed and the data is all the information of when the event was invoked
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The value of the element where the event occurred it is not the element that the event listener is applied on
We can check by console logging the target property of the event object
We can either check the code or MDN for more information
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The first one is a callback function and the second is a function call with no argument. Because of this it runs the function with nothing but the first one will run with the event as it’s argument
What is the className property of element objects?
A string of the class attribute of an element It can get or set the className attribute on an HTML document
How do you update the CSS class attribute of an element using JavaScript?
You use the .className on the variable storing the DOM information and assign it a string value of the new class name
What is the textContent property of element objects?
The textContent property is the text content of a node on the DOM
Property that stores any text content that is present in the node