Dom manipulation Flashcards
Why do we log things to the console?
To check them
What is a “model”?
a version of the page in memory structured to be a DOM tree
Which “document” is being referred to in the phrase Document Object Model?
the web page / the tree structure for the web page
What is the word “object” referring to in the phrase Document Object Model?
all HTML tags make up the DOM tree as it sees tags as objects / all elements that go into the DOM tree
What is a DOM Tree?
it is made up of all the HTML tags / how the DOM structures the model of the web page / kinda like an organization chart CEO at top and alot of workers at the bottom
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?
so it doesn’t have to be searched for every time you want to use it.
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?
so all the HTML loads before it
What does document.querySelector() take as its argument and what does it return?
takes a string or css selector / returns the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
takes a string or css selector / returns all the matching elements as a NodeList
Why do we log things to the console?
to check them
What is the purpose of events and event handling?
so we know what the user is doing and how to handle it
Are all possible parameters required to use a JavaScript method or function?
no
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 into another function as an argument
What object is passed into an event listener callback when the event fires?
event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
check by using console.log / console.dir / or mdn
reference to the object onto which the event was dispatched.
element that is being interacted with
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
the bottom code calls the function as soon as it loads instead of by the user or when the event is triggered
What is the className property of element objects?
className is the attribute for the element
How do you update the CSS class attribute of an element using JavaScript?
with the className property and the assignment operator / without = you would just get a list and can log it
What is the textContent property of element objects?
The textContent property allows you to
collect or update just the text that is in the
containing element (and its children).
How do you update the text within an element using JavaScript?
by changing the textContent property of an element
Is the event parameter of an event listener callback always useful?
Not always useful or needed
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
maybe a little bit harder as you couldn’t visually see how many clicks have been done
Why is storing information about a program in variables better than only storing it in the DOM?
So you can can update them as you go on
/ so you dont have to keep calling on 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 ?
submit
What does the event.preventDefault() method do?
prevents the event from being handled with its default behavior
What does submitting a form without event.preventDefault() do?
reloads the page and erases the info in the form
What property of a form element object contains all of the form’s controls.
elements
What property of form a control object gets and sets its value?
the value property
What is one risk of writing a lot of code without checking to see if it works so far?
that you don’t know what is it that isn’t working
What is an advantage of having your console open when writing a JavaScript program?
you can check as you go along and debug
Does the document.createElement() method insert a new element into the page?
No it only creates an element
How do you add an element as a child to another element?
append child
What do you pass as the arguments to the element.setAttribute() method?
2 params the first is the attribute and the second is the value
What steps do you need to take in order to insert a new element into the page?
create the element, set attributes if any, add text content and then append it
What is the textContent property of an element object for?
to add text to the newly created element
Name two ways to set the class attribute of a DOM element.
with setAttribute / classList / className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
to procedurally create multiple elements and add them to a page.
to dynamically create elements on a page
What is the event.target?
what is being target / element that the user interacted with
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling / goes up to see if an eventListener is added to any of the parents
What DOM element property tells you what type of element it is?
event.target.tagName
What does the element.closest() method take as its argument and what does it return?
a selector and returns the closest element with the selector
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?
adding it to a parent / container
What is the event.target?
what is being target / element that the user interacted with
What is the affect of setting an element to display: none?
it doesnt display / doesnt show up
What does the element.matches() method take as an argument and what does it return?
a selector and returns a boolean value (checks if what your checking matches what your returning)
How can you retrieve the value of an element’s attribute?
element.getAttribute
At what steps of the solution would it be helpful to log things to the console?
at every step so you know if your making a mistake
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?
you would have to add an eventListener to every tab that you have and will add
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 many more if statements to check for each tab/view and add more if you add more on the page
How to you store data in localStorage?
with localStorage.setItem(key,value)
How to you retrieve data from localStorage?
with localStorage.getItem(key)
What data type can localStorage save in the browser?
JSON strings
When does the ‘beforeunload’ event fire on the window object?
if the user tries to leave the page
The beforeunload event is fired when the window, the document and its resources are about to be unloaded.