DOM Flashcards
Why do we log things to the console?
To see what is going on, for debugging purposes
What is a “model”?
A representation of something
Which “document” is being referred to in the phrase Document Object Model?
the webpage itself
What is the word “object” referring to in the phrase Document Object Model?
the HTML page
What is a DOM Tree?
The pathways of elements and their children
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 prevent repeated queries to the DOM, just reuse the variable
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?
To allow the HTML code to be parsed first, then JavaScript can read them
(The elements need to be put on the DOM first, then moved around with JS)
What does document.querySelector() take as its argument and what does it return?
CSS selector as argument, and returns the location of the element selected
What does document.querySelectorAll() take as its argument and what does it return?
CSS selector as argument, and returns a Node List
Why do we log things to the console?
For debugging, check results of javascript code
What is the purpose of events and event handling?
Listening for user interactions with the page
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?
When a function is passed into anothe function or method
What object is passed into an event listener callback when the event fires?
The event object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
The element at the point of interaction in the DOM
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
1st one sets handleClick as a callback function. 2nd one calls the function within the arguments, thus passing the return value as the second argument.
What is the className property of element objects?
A property that receives and assigns the class attribute of the target element
How do you update the CSS class attribute of an element using JavaScript?
object.classname = ‘updated class attribute’;
What is the textContent property of element objects?
It represents the text content of a node and its descendants
How do you update the text within an element using JavaScript?
object.textContent = ‘New 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?
It is easier to manage variables than having to query the DOM repeatedly.
What event is fired when a user places their cursor in a form control?
focus event
What event is fired when a user’s cursor leaves a form control?
blur event
What event is fired as a user changes the value of a form control?
input event
What event is fired when a user clicks the “submit” button within a ?
submit event
What does the event.preventDefault() method do?
It prevents the page from reloading after the submit event
What does submitting a form without event.preventDefault() do?
It would reload the page after the submit event
What property of a form element object contains all of the form’s controls.
form element’s elements property
What property of a form control object gets and sets its value?
value attribute
What is one risk of writing a lot of code without checking to see if it works so far?
Not being able to see which part of the code is creating a possible error
What is an advantage of having your console open when writing a JavaScript program?
Being able to check for errors, and confirming values are printing as expected while code is being written
Does the document.createElement() method insert a new element into the page?
No, it just creates the element. appendChild() must be used to insert into the page.
How do you add an element as a child to another element?
parentChild.appendChild(childElement)
appends the child as the last child of the parentChild
What do you pass as the arguments to the element.setAttribute() method?
attribute type, attribute value
What steps do you need to take in order to insert a new element into the page?
createElement()
appendChild() to an existing element in the page
append() can also be used (check MDN)
What is the textContent property of an element object for?
To set or change the text content of an element
text content can be created and assigned to a new element
using textContent does destroy other child elements that currently exists in the designated element
Name two ways to set the class attribute of a DOM element.
setAttribute(class, )
object.className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
A function can be repeated for different object values, and the return value of the function can be easily managed like a variable
What is the event.target?
the element that was interacted with in the webpage
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
.nodeName is also applicable, but .tagName is needed to retrieve an actual element
What does the element.closest() method take as its argument and what does it return?
It takes a CSS Selector as its argument and returns the node that matches that CSS Selector
How can you remove an element from the DOM?
querySelector to find the element node
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?
use addEventListener on the parent element