DOM Flashcards
Why do we log things to the console?
to verify code and get information about code
What is a “model”?
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?
data type
What is a DOM Tree?
element + content and configuration
Give two examples of document methods that retrieve a single element from the DOM.
getElementByID or querySelector
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementsByClassName, getElementsByTagName, querySelectorAll
Why might you want to assign the return value of a DOM query to a variable?
to reuse in the future (stores location of DOM)
What console method allows you to inspect the properties of a DOM element object?
console.dir
Why would a
tag need to be placed at the bottom of the HTML content instead of at the top?
load the content of all the elements, documents load from top to bottom
What does document.querySelector() take as its argument and what does it return?
css selector syntax (string), returns only the first element that matches
What does document.querySelectorAll() take as its argument and what does it return?
css selector (string), returns NodeList
What is the purpose of events and event handling?
when an event “fires” , it can be used to trigger a particular function or code
Are all possible parameters required to use a JavaScript method or function?
no, not all parameters are required
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
passing the definition of the function, not the return
What object is passed into an event listener callback when the event fires?
event object that has data about the event that occured
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
event.target is the target element being used / originated from
console.log(event.target)
MDN
What is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())
first code is using the handleClick function while the second code is calling the function
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
What is the textContent property of element objects?
contains
How do you update the text within an element using JavaScript?
element.textcontent = ‘string’
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?
What event is fired when a user places their cursor in a form control?
focus
What event is fired when a user’s cursor leaves a form control?
blur
What event is fired as a user changes the value of a form control?
input
What event is fired when a user clicks the “submit” button within a <form>?
submit
What does the event.preventDefault() method do?
prevents default action of the event
What does submitting a form without event.preventDefault() do?
browser would reload the page with form’s values in the URL
What property of a form element object contains all of the form’s controls.
.elements
What property of a form control object gets and sets its value?
.value
What is one risk of writing a lot of code without checking to see if it works so far?
could be writing bugs
What is an advantage of having your console open when writing a JavaScript program?
when writing code, you can see errors as they happen
Does the document.createElement() method insert a new element into the page?
no, created and stored in a variable
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, value (attribute name, value)
ex. .setAttribute(‘class’, ‘click-button’)
What steps do you need to take in order to insert a new element into the page?
create the element and store in a variable, give it content, call querySelector to find element and store in variable, append child and add it to the dom
What is the textContent property of an element object for?
to return all the text content including all elements (set or get)
Name two ways to set the class attribute of a DOM element.
.className property & .setAttribute() method
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
only have to write function once so it can be reusable, and have a name to reuse the function
What is the event.target?
where the event started
element u clicked on
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling
What DOM element property tells you what type of element it is?
.tagName
all caps
What does the element.closest() method take as its argument and what does it return?
string of a valid css selector
return closest ancestor element or itself
How can you remove an element from the DOM?
.remove() method
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?
add event listener to an ancestor