DOM Flashcards
Why do we log things to the console?
To make sure our code works
What is a “model”?
A representation of an object that is typically smaller than the object that is being modeled
Which “document” is being referred to in the phrase Document Object Model?
The html document being loaded into a browser
What is the word “object” referring to in the phrase Document Object Model?
Each individual elements on the html page (including the document itself)
What is a DOM Tree?
The drawn mapping of the elements of the html page
Give two examples of document methods that retrieve a single element from the DOM.
querySelector(‘css selector’)
getElementById(‘id’)
Give one example of a document method that retrieves multiple elements from the DOM at once.
querySelectorAll(‘css selector’)
getElementsByClassName(‘class’)
Why might you want to assign the return value of a DOM query to a variable?
Why might you want to assign the return value of a DOM query to a variable?
What console method allows you to inspect the properties of a DOM element object?
console.dir(variableName)
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
The browser needs to parse all of the elements in the HTML page before the JavaScript code can access them.
What does document.querySelector() take as its argument and what does it return?
It takes any valid CSS selector as its argument
Returns the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
It takes any valid CSS selector as its argument
Returns all matching elements in a node list
Why do we log things to the console?
To make sure our code works
What is the purpose of events and event handling?
Events are any changes to a document whether it be from user interaction or changes from within the browser.
Event handlers trigger functions depending on the event
What do [] square brackets mean in function and method syntax documentation?
They represent optional arguments
What method of element objects lets you set up a function to be called when a specific type of event occurs?
element.addEventListener(‘event’, callbackFunction);
What is a callback function?
A function that is passed as an argument in another function’s call
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?
It’s full html element being targeted by a dom query
You could check by logging onto the console or check mdn documentation
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The first one takes a callback function as the argument and the second one runs the function and whatever that is returned will be used as the argument for the addEventListener method
What is the className property of element objects?
It accesses the class attribute of an html element
How do you update the CSS class attribute of an element using JavaScript?
Query the dom for an element then use the className property to assign a string value to update the class name
What is the textContent property of element objects?
It accesses all text within the opening and closing tags of an element
How do you update the text within an element using JavaScript?
Query the dom for an element then use the textContent property to assign a string value to update the text content
Is the event parameter of an event listener callback always useful?
No, sometimes you’ll want to write code that applies to other parts of the html document not pertaining to the element the event it targeting
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. Probably would have to use a loop
Why is storing information about a program in variables better than only storing it in the DOM?
If you store information in the DOM, other parts of your javascript code could potentially change it
Does the document.createElement() method insert a new element into the page?
No it only creates a reference to a dom element
How do you add an element as a child to another element?
$parent.appendChild($newElement)
What do you pass as the arguments to the element.setAttribute() method?
2 arguments: first argument is the html attribute, second argument is the value
What steps do you need to take in order to insert a new element into the page?
createElement(), setAttribute()(optional), createTextNode()(optional), appendChild()
What is the textContent property of an element object for?
Sets or gets the text within the opening and closing tags of an element
Name two ways to set the class attribute of a DOM element.
setAttribute(‘class’, ‘value’)
$element.className = ‘value’
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
You can reuse that function in several parts of your code
It’s easier to debug a function rather than a whole js document
What is the event.target?
It references the object in which the event was directed at
Why is it possible to listen for events on one element that actually happen on its descendent elements?
Using event flow, you can bubble up to the descendent’s ancestor
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?
It takes a selectors as an argument and
It returns the closest element with that selector name to the element that called closest()
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?
Event delegation