DOM Flashcards
Why do we log things to the console?
To check that our code is doing what we intended for it to do
What is a “model”?
Representation of how the html page is structured
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?
Datatype object (in JavaScript)
What is a DOM Tree?
Collection of html elements (like a family tree) AND all of its child elements as well as any relevant information attached to them
Give two examples of document methods that retrieve a single element from the DOM.
o getElementbyID(‘id’) o querySelector(‘css selector’)
Give one example of a document method that retrieves multiple elements from the DOM at once.
o getElementsByClassName(‘class’) o getElementsByTagName(‘TagName’) o querySelectorAll(‘css selector’)
Why might you want to assign the return value of a DOM query to a variable?
To allow browser to locate the element faster in the DOM (if you’re going to be using this return value more than once)
What console method allows you to inspect the properties of a DOM element object?
console.dir( )
Why would a script tag need to be placed at the bottom of the HTML content instead of at the top?
Because the browser needs to read all the elements in the HTML page before JavaScript can access them
What does document.querySelector() take as its argument and what does it return?
It takes a STRING containing a CSS selector and returns the first DOM element object with that selector.
What does document.querySelectorAll() take as its argument and what does it return?
It takes a STRING containing a CSS selector and gives a NodeList of elements.
What is the purpose of events and event handling?
To make webpages interactive
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 that you pass inside another function as an argument
What object is passed into an event listener callback when the event fires?
Event object (object passed by JS that has all the information 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?
A read-only property that shows which object(or html element) triggered the event
Check through the console log and see which element gets logged
What is the difference between these two snippets of code?
- element.addEventListener(‘click’, handleClick)
- element.addEventListener(‘click’, handleClick())
1 has a callback function
2 has the return of the function call
What is the className property of element objects?
Sets and returns the class name of a specified element
How do you update the CSS class attribute of an element using JavaScript?
By using element.className = ‘new-class-name-in-html-format’
What is the textContent property of element objects?
Represents the text content of the node
How do you update the text within an element using JavaScript?
By using node.textContent = ‘newtext’
Is the event parameter of an event listener callback always useful?
No sometimes you have the information you need without the parameters
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
Complicated- because it wouldn’t be easy to tell what our current number is at
Why is storing information about a program in variables better than only storing it in the DOM?
Easy to make future updates when information is easily visible and accessible
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 normally taken by the implementation of a method from happening
What does submitting a form without event.preventDefault() do?
When the submit button is clicked, the page will reload immediately
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
Does the document.createElement() method insert a new element into the page?
No, because it needs to be added to the DOM for it to be visible
How do you add an element as a child to another element?
.appendChild( insert new child element)
What do you pass as the arguments to the element.setAttribute() method?
setAttribute(‘name’, value)
What steps do you need to take in order to insert a new element into the page?
o Create the element: document.createElement() o Save(assign) element to the variable o Append the element into a parent node: .appendChild()
What is the textContent property of an element object for?
For inserting text into a node and its children
Name two ways to set the class attribute of a DOM element.
o element.ClassName = ‘whatever-class-name’ o setAttribute('name', value)
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
o You can create a bulk of html code to insert all at once
o Function can be reused
What is the event.target?
Read-only property that tells you which element the event occurred (dispatched from) on
Why is it possible to listen for events on one element that actually happen its descendent elements?
Due to event bubbling as it continues to look up the ancestral tree
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?
o Takes a string with a css selector
o Returns the closest element that matches that selector
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?
By using an if statement to check if the event.target property is equal to the actual clicked element (mostly element but it doesn’t have to be strictly only an element – could be something else)
What is the event.target?
Read-only property that tells you which element the event occurred (dispatched from) on
What is the effect 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?
o String of a css selector (in a css form)
o Gives back a Boolean value
How can you retrieve the value of an element’s attribute?
.getAttribute(‘string of the attribute you want to get the value of’)
At what steps of the solution would it be helpful to log things to the console?
After 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?
You’d have to write an individual function for each tab
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’d have to write multiple if-else statements for each views