DOM Flashcards
What is a “model”?
a representation of something
Which “document” is being referred to in the phrase Document Object Model?
html
What is the word “object” referring to in the phrase Document Object Model?
object data type
What is a DOM Tree?
javascript object for an html element and its children containing attributes and values
Give two examples of document methods that retrieve a single element from the DOM.
querySelector( )
getElementById( )
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 use in the future and easier to find
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?
HTML content has to load first
What does document.querySelector( ) take as its argument and what does it return?
string that contains CSS selector and returns DOM at first occurrence
What does document.querySelectorAll( ) take as its argument and what does it return?
string that contains CSS selector and returns Node list
What is the purpose of events and event handling?
interactivity
Are all possible parameters required to use a JavaScript method or function?
no, some parameters are optional
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?
function passed as an argument in another function
What object is passed into an event listener callback when the event fires?
object with all the data about
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
element WHERE the event occurred, check on MDN
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
function definition return value of the function call
What is the className property of element objects?
he value of an element’s class attribute
How do you update the CSS class attribute of an element using JavaScript?
object.className = ‘class’
What is the textContent property of element objects?
represents the text content of the node and its descendants
How do you update the text within an element using JavaScript?
object.textContent = ‘text content’
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?
its easier to access and pass as arguments
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 ?
submit
What does the event.preventDefault() method do?
prevents the default behavior for the event
What does submitting a form without event.preventDefault() do?
page refreshes
What property of a form element object contains all of the form’s controls.
elements property
What property of form a control object gets and sets its value?
value property
What is one risk of writing a lot of code without checking to see if it works so far?
harder to spot bug
What is an advantage of having your console open when writing a JavaScript program?
you know when somethings not working
Does the document.createElement() method insert a new element into the page?
no, it just creates it
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’, ‘value of attribute’)
What steps do you need to take in order to insert a new element into the page?
.createElement(), store new element, query for parent element, append onto parent
What is the textContent property of an element object for?
retrieve the text or change the text
Name two ways to set the class attribute of a DOM element.
setAttribute() or .className
What is the event.target?
element where the event occurred
Why is it possible to listen for events on one element that actually happen its descendent elements?
bubbling
What DOM element property tells you what type of element it is?
.tagName
What does the element.closest() method take as its argument and what does it return?
takes string of selector as argument and returns closest ancestor of the selected element
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?
addEventListener to parent event and check if type of event.target is what you want it on
What is the affect of setting an element to display: none?
does not display and taken out of document flow
what does the element.matches() method take as an argument and what does it return?
it takes a selector in a string and returns a boolean
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 where a value is changing
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?
make new event handler each time you add
What is a “callback” function?
function passed into another function as an argument
Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?
setTimeout( )
How can you set up a function to be called repeatedly without using a loop?
setInterval( )
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
an positive integer ID used to pass to clearInterval ( )