DOMS Flashcards
Why do we log things to the console?
To debug and verify
What is a “model”?
A representation of something.
Which “document” is being referred to in the phrase Document Object Model?
It refers to the fact that each node is an object.
Node (for our purposes most likely refers to an HTML element)
What is a DOM Tree?
An element and all of its content
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?
You might need to work with an element multiple times, so the computer doesn’t have to constantly look for that reference to the variable.
What console method allows you to inspect the properties of a DOM element object?
dir
why would a script tag need to be placed at the bottom of the HTML content instead of the top?
Because if you put it on the top, the html code isn’t loaded yet, and you can’t access the elements.
What does document.querySelector() take as its argument and what does it return?
CSS selector
returns the JS object
What does document.querySelectorAll() take as its argument and what does it return?
CSS selector
returns a node list
Why do we log things to the console?
Debugging and verification
What is the purpose of events and event handling?
When you want to do something when something else occurs.
What method of element objects let you set up a function to be called when a specific type of event occurs?
addEventListener()
What is a callback function?
It is a function used as an argument in another function.
What object is passed into an event listener callback when the event fires?
It is the object of the event that was used
e.g. function handleDoubleClick(event) {}
You don’t define the event parameter yourself
- JavaScript prepacks it with all the information about the type of event handleDoubleClick is used with addEventListener
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The event target is a reference to the object that caused the event to happen.
use the console and look at MDN
The target is where the event directly occurred from.
What is the difference between these two snippets of code?
o element.addEventListener(‘click’, handleClick)
o element.addEventListener(‘click’, handleClick())
o element.addEventListener(‘click’, handleClick)
- runs when the event happens
o element.addEventListener(‘click’, handleClick())
- runs when the page loads
What is the className property of element objects?
It is the value of the class attribute.
How do you update the CSS class attribute of an element using JavaScript?
variableName.className = “new class”
variableName.classList.add(‘newClass’)
variableName.classList.remove(‘removeThisClass’)
What is the textContent property of element objects?
It gives you access to the text content within an element.
How do you update the text within an element using JavaScript?
variableName.textContent = new value
Is the event parameter of an event listener 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?
It lets you quickly access it whenever you need it.
What event is fired when a user places their cursor in a form control?
focus
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?
It stops the event from doing what it normally does?
What does submitting a form without event.preventDefault() do?
The data will be sent somewhere and the page will reload
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?
.value
Does the document.createElement() method insert a new element into the page?
no
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?
attribute name and value
What steps do you need to take in order to insert a new element into the page?
Create element node
Retrieve the element you want to append to the node
Append the element to the node
Add node to DOM tree
What is the textContent property of an element object for?
Retrieve or change text content in a node
Name two ways to set the class attribute of a DOM element
setAttribute()
className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
You can reuse the function.
It makes code more readable.
What is the event.target
It is where the event occurred from
Why is it possible to listen for events on one element that actually happens its descendent elements?
event 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?
Selector
returns the closest node that matches the given selector up the ancestor
How can you remove an element from the DOM?
.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?
You put the event on the least specific node (probably the container)
What is the event.target?
It is the element where the event occurred from
What is the effect of setting an element to display: none?
It makes the element act as if it doesn’t exist
What does the element.matches() method take as an argument and whatdoes it return?
selector
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?
When you need to see if something is working the way you intend it to