DOM Flashcards
Why do we log things to the console?
In order to check and debug our work
What is a “model”?
DOM is a representation (a separate entity)
Which “document” is being referred to in the phrase Document Object Model?
DOCTYPE Html
What is the word “object” referring to in the phrase Document Object Model?
Data type (or type of Data)
What is a DOM Tree?
Is a JS object for html element including its elements, attributes, values, children, and its children.
Give two examples of document methods that retrieve a single element from the DOM.
getElementByID
querySelector
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?
Easier to retrieve data, which makes it easy to retrieve
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?
Html loads from top to bottom
Needs to parse all the elements before
What does document.querySelector() take as its argument and what does it return?
Returns only the first of the matching elements (selectors for css)
What does document.querySelectorAll() take as its argument and what does it return?
Returns all of that matches the called element (selector for css)
What is the purpose of events and event handling?
To create a reaction from the user upon action
Are all possible parameters required to use a JavaScript method or function?
Not always necessary
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?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
It’s in the event listener
What object is passed into an event listener callback when the event fires?
Is an object with huge amount of properties that just occurred (storing lots of data)
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
Is the point of interaction
Element that dispatches the event
Console.log where the event occurred
If there is a div… and there is a button… the target will be button
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
handleClick - is a callback function
element.addEventListener(‘click’, handleClick()):
Functions values get RETURNED
This will be undefined
ex) if it gets checked… and stops
EventListener will never be called by user. It’s called by JS
What is the className property of element objects?
It’s the property of the element that sets the value of class attribute ex) gettingHot.className This holds the class name value Get a new value or set a new value
How do you update the CSS class attribute of an element using JavaScript?
You would assign a new value to the object.className or variable.className
Simply assign new value to it
What is the textContent property of element objects?
Get or set
textContent property represents the text content of the node and its descendants
textContent returns every element in the node…
(innerText will not return the text of hidden elements)
Example: Var example = document.getElementById(‘diva’).textContent
How do you update the text within an element using JavaScript?
You assign the query the DOM to a variable
And assign new variable.ClassName to a new string
Is the event parameter of an event listener callback always useful?
yes
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
Simpler with variables
Why is storing information about a program in variables better than only storing it in the DOM?
Because you can manipulate the variables to be edited and store functions to use for later
Example is countingClicks.textContent = ‘Clicks: ‘ + storageNumberOfClicks
Does the document.createElement() method insert a new element into the page?
No, it is not added yet
The element node is created
This is stored in a variable
How do you add an element as a child to another element?
Use the .append() method
What do you pass as the arguments to the element.setAttribute() method?
With its name and value
What steps do you need to take in order to insert a new element into the page?
Step 1: createElement() method
Step 2: createTextNode() method (giving it content)
Step 3: appendChild() method (adding to DOM tree, specifies which element)
What is the textContent property of an element object for?
For strings
Name two ways to set the class attribute of a DOM element.
className method
setAttribute method
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
You can loop through each of the items and manipulate it.
What is the event.target?
The target property of the Event interface is a reference to the object onto which the event was dispatched