DOM Flashcards
Why do we log things to the console?
For debugging and/or making sure things work
What is a “model”?
Layout of the html document as a tree of objects
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?
HTML elements as objects
What is a DOM Tree?
The HTML document in the form of a model of objects that looks like a tree
Give two examples of document methods that retrieve a single element from the DOM.
document.queryselector()
document.getElementById()
Give one example of a document method that retrieves multiple elements from the DOM at once.
document.querySelectorAll()
Why might you want to assign the return value of a DOM query to a variable?
So you can access it later
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?
If it was at the top, the website would stop loading until all of the javascript was downloaded
What does document.querySelector() take as its argument and what does it return?
CSS selector as argument
Returns an element object representing the first element in the document that matches the selector
What does document.querySelectorAll() take as its argument and what does it return?
CSS selector as argument
Returns a NodeList containing one element object for each element that matches the selector
What is the purpose of events and event handling?
Handler is called by listener (I write handler)
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?
Event listener
What is a callback function?
What object is passed into an event listener callback when the event fires?
event
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
Returns the element that triggered the event
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 behavior of the event
What does submitting a form without event.preventDefault() do?
refreshes the page
What property of a form element object contains all of the form’s controls.
form.elements
What property of a form 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?
Bugs/errors
What is an advantage of having your console open when writing a JavaScript program?
To check for bugs/errors
Does the document.createElement() method insert a new element into the page?
It doesn’t until it is appended
How do you add an element as a child to another element?
document.appendChild()
What do you pass as the arguments to the element.setAttribute() method?
Name of the attribute, value
What steps do you need to take in order to insert a new element into the page?
createElement -> append
What is the textContent property of an element object for?
The text for the element
Name two ways to set the class attribute of a DOM element.
classname or setAttribute or classList
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
You can create it for multiple items at once, and since it’s a function it will be there for later use?
What is the event.target?
It’s what element the user interacted with
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?
event.target.tagName
What does the element.closest() method take as its argument and what does it return?
Css selector and returns the closest upward element or parent selected
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?
Adding an event listener to the parent element
What is the affect of setting an element to display: none?
Removed from page
What does the element.matches() method take as an argument and what does it return?
make sure an element has a css selector
How can you retrieve the value of an element’s attribute?
.getAttribute()
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?
Separate event handlers for each event listener
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?
Individual conditionals