DOM Flashcards
What is a “model”?
A template to follow
Which “document” is being referred to in the phrase Document Object Model
the current html document
What is the word “object” referring to in the phrase Document Object Model?
The elements of the document are represented as objects
What is a DOM tree?
A visual representation of all of the document’s nodes and their relation to one another
Give two examples of document methods that retrieve a single element from the DOM.
document. querySelector(‘css selector’)
document. getElementById(‘id’)
Give one example of a document method that retrieves multiple elements from the DOM at once.
document. querySelectorAll( )
document. getElementsByClassName( )
document. getElementsByTagName( )
Why might you want to assign the return value of a DOM query to a variable?
The variable does not hold the value of the node itself, but a reference to where that node is stored. So, if you want to do multiple things to that node, it is useful to store it in a variable so that the computer does not have to search for it every time
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?
The browser needs to parse through the entire HTML body
What does document.querySelector( ) take as its argument and what does it return?
It takes in a CSS selector and returns the first element that matches the result
What does document.querySelectorAll( ) take as its argument and what does it return?
CSS selector as argument
Returns a NodeList of all elements that match
What is the purpose of events and event handling?
Makes the page more interactive by triggering scripts when certain events happen
What method of element objects lets you set up a function to be called when a specific type of event occurs?
.addEventListener(event, callback function)
What is a callback function?
A function that is passed as an argument into another function.
A function that you don’t want to call right away so you store for later
What object is passed into an event listener callback when the event fires?
the event object
What is the event.target? If you’re not sure, where would you find information about it?
property that returns the object which the event was bounded onto
Difference between:
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
The first one is a callback function; Only runs when the event occurs
Second one is a normal function call and it runs as the web page is loaded only.
What is the className property of element objects?
returns a string of the value of the class attribute for the element
How do you update the CSS class attribute of an element using JavaScript?
update the className property of the element
What is the textContent property of element objects?
Gets the text content of the element as well as the text content of its children(?)
How do you update the text within an element using JavaScript?
with the .textContent property of an element
Is the event parameter of an event listener callback always useful?
No, but always present. Do not forget event parameter when defining event handler functions
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
More difficult as the click counter allows us to control both the text content and the button color
Why is storing information about a program in variables better than only storing it in the DOM?
It is easier to reference especially if using multiple times
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 form?
‘submit’ event
What does the event.preventDefault( ) do?
tells browser that even if an event does not get explicitly handled, its default action should not be taken
What does submitting a form without event.preventDefault( ) do?
The page will refresh with the user’s inputs added to the URL
What property of a form element object contains all of the form’s controls?
form.elements
array-like object(?) that you can access via indexes
What property of a form control object lets you get and set its value?
.value
What is one risk of writing a lot of code without checking to see if it works so far?
Will inevitably run into bugs/errors and it will be much harder to debug if there’s a lot of code to go through
What is an advantage of having your console open when writing a JavaScript program?
Can see if any errors are occurring and confirm the program is running as expected.
Does the document.createElement( ) method insert a new element into the page?
No; need the appendChild( ) method
How do you add an element as a child to another element?
parent-element.appendChild(aChild)
.append( )
What do you pass as the arguments to the element.setAttribute( ) method?
setAttribute(attribute, value)
What steps do you need to take in order to insert a new element into the page?
Create a new element and assign it to a variable
Use the variable to assign textContent to it
append the new element onto a parent with appendChild
What is the textContent property of an element object for?
Gets or sets the text node of an element
Name two ways to set the class attribute of a DOM element
element. className
element. setAttribute( )
element. classList.remove( )
What are two advantages of defining a function to create something (like the work of creating a DOM tree?)
Helps break down the work you need into chunks as well as allow you to re-use/repeat the function as many times as needed
Give two examples of media features that you can query in an @media rule?
width
orientation
Which HTML meta tag is used in mobile-responsive web design?
meta name=”viewport” content=”width=device-width, initial-scale=1.0”
What is the event.target?
the element that the user actually interacted with, but as a result of event bubbling, it caused an event to get triggered
Why is it possible to listen for events on one element that actually happen to its descendant elements?
Event bubbling
What DOM element property tells you what type of element it is?
element.tagName
What does the element.closest( ) method take as its argument and what does it return?
takes in a css selector as argument and returns the closest ancestor that matches
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?
By adding the event listener to a parent and then relying on event bubbling and conditions to find the actual element you want