DOM Flashcards
Why do we log things to the console?
to spot check that our code works and debug it, verification, landmark to see code execute.
What is a “model”?
a representation of something.
Which “document” is being referred to in the phrase Document Object Model?
the HTML document. the entire page.
What is the word “object” referring to in the phrase Document Object Model?
refers to the data type object within javascript.
What is a DOM Tree?
an element plus all of its children and their configuration.
Give two examples of document methods that retrieve a single element from the DOM.
getElementById(), querySelector()
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?
to store the reference/location of the object in the DOM tree (caching). it saves the browser looking through the DOM tree to find the same element again.
What console method allows you to inspect the properties of a DOM element object?
console.dir(object)
Why would a
tag need to be placed at the bottom of the HTML content instead of at the top?
it speeds up page load times by loading the HTML content first before running DOM scripts.
What does document.querySelector() take as its argument and what does it return?
it takes CSS selectors as a string as its argument and returns the first matching element.
What does document.querySelectorAll() take as its argument and what does it return?
it takes CSS selectors as a string as its argument and returns a nodelist of all the elements.
What is the purpose of events and event handling?
to prepare to trigger a function and make the website more interactive
Are all possible parameters required to use a JavaScript method or function?
No, there are optional parameters.
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?
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?
information about what happens.
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
it holds the element where the information originated from. you can check with console log and find more information on MDN.
What is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())
the parentheses are omitted. parentheses indicate that the function should run as the page loads rather than when the event fires.
What is the className property of element objects?
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?
element.className = ‘new class name’
What is the textContent property of element objects?
allows you to collect or update just the text in its containing element
How do you update the text within an element using JavaScript?
element.textContent = ‘new text’
Is the event parameter of an event listener callback always useful?
No
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
More complicated
Why is storing information about a program in variables better than only storing it in the DOM?
By keeping data in javascript you don’t make your data dependent on another language in the DOM.
Does the document.createElement() method insert a new element into the page?
No. it is stored in a variable until attached to DOM tree later with appendChild.
How do you add an element as a child to another element?
appendChild()
What do you pass as the arguments to the element.setAttribute() method?
name (of attribute) and value
What steps do you need to take in order to insert a new element into the page?
create element, store in a variable, DOM query, appendChild
What is the textContent property of an element object for?
it represents the text content of the node and its descendants
Name two ways to set the class attribute of a DOM element.
setAttribute() and .className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
You can reuse it later on without having to create a new DOM tree.
you can give it the function a name so it’s easier to track.
What is the event.target?
it holds the element where the information originated from
Why is it possible to listen for events on one element that actually happen its descendent elements?
Event bubbling flow.
What DOM element property tells you what type of element it is?
event.target.tagName … tagName is in all caps.
What does the element.closest() method take as its argument and what does it return?
selectors - a string of a valid CSS selector
returns the closest ancestor element or itself, which matches the selectors. If none, returns null.
How can you remove an element from the DOM?
element.remove()
If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?
By adding the event listener to an ancestor element.
What is the affect of setting an element to display: none?
it removes the element from the document flow.
What does the element.matches() method take as an argument and what does it return?
selectors (a string containing valid CSS selectors) to test against element
returns true if element matches selector, false otherwise
How can you retrieve the value of an element’s attribute?
getAttribute()
At what steps of the solution would it be helpful to log things to the console?
every step
If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?
It would have to have a function and event listener for each tab and view
If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?
you would have to write a function and event listener for each tab and view and have a conditional statement for each function to hide the other tabs.