DOM Flashcards
Why do we log things to the console?
to keep a record of the output (self-check)
What is a “model”?
a representation of the the actual thing, recreation of something
Which “document” is being referred to in the phrase Document Object Model?
it would be the HTML document
What is the word “object” referring to in the phrase Document Object Model?
the data type (elements) from the HTML
What is a DOM Tree?
it is a layout like a family tree that consists of HTML elements and how they connect to each other. Body element with all of its child elements and the content it holds.
Give two examples of document methods that retrieve a single element from the DOM.
getElementById, querySelector(‘CSS selector’)
Give one example of a document method that retrieves multiple elements from the DOM at once.
getElementByClassName(‘class’),
Why might you want to assign the return value of a DOM query to a variable?
in the future for reference, it is much easier and efficient to assign it to a variable for faster access.
What console method allows you to inspect the properties of a DOM element object?
console.dir; the directory method of the console object.
Why would a tag need to be placed at the bottom of the HTML content instead of at the top?
the browser reads the code from top to bottom. In the JS when referring to elements (objects) there is nothing to refer to.
What does document.querySelector() take as its argument and what does it return?
it takes a string which is a CSS selector as arguments and returns the first element that matches the selector
What does document.querySelectorAll() take as its argument and what does it return?
it takes a selector as an argument and returns a list of properties of that object which is called a node list.
Why do we log things to the console?
to keep track of our output and see if our code at each code
What is the purpose of events and event handling?
it is for a function or action to be performed when and after a user interacts with the page itself.
Are all possible parameters required to use a JavaScript method or function?
No, not all the parameters are needed (boolean).
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.
What object is passed into an event listener callback when the event fires?
it is the object created by the JS engine and relays all the information about the object
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
it is the read-on property target of the object event. You could check the console. Go onto MDN. Where the event occurred (started).
What is the difference between these two snippets of code?
element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())
First, the second argument is a callback function. Second, the second argument is the return of the function is being called, which makes the function work. We cannot pass the work as an argument.
What is the className property of element objects?
The className property of the Element interface gets and sets the value of the class attribute of the specified element. Format is a string and the this needs to be the full string.
How do you update the CSS class attribute of an element using JavaScript?
using the property.className = “and new value of that property.
What is the textContent property of element objects?
it represents the text content of the node and its descendants.
How do you update the text within an element using JavaScript?
by using the text.Content property
Is the event parameter of an event listener callback always useful?
not always useful
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 because wouldnt be able to keep track of the # of clicks, and would have to go back into the DOM to retrieve that element
Why is storing information about a program in variables better than only storing it in the DOM?
You are able to access it in the future. easy to update and visible.
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 ?
submit
What does the event.preventDefault() method do?
stops the page from refreshing…The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.
checkbox example on MDN
What does submitting a form without event.preventDefault() do?
does not refresh the page.
What property of a form element object contains all of the form’s controls.
elements property which are input, button, textarea….
What property of a form control object gets and sets its value?
.value
What is one risk of writing a lot of code without checking to see if it works so far?
ultimately there is a chance it wont work, so checking it helps you know what exactly does not work. try to use console.log!
What is an advantage of having your console open when writing a JavaScript program?
Being able to see if anything goes wrong with the code. being able to see if an error is about to happen when saving.
Does the document.createElement() method insert a new element into the page?
no, it needs to be appended to the parent element first or the DOM tree
How do you add an element as a child to another element?
.appendChild() to the parent element
What do you pass as the arguments to the element.setAttribute() method?
the keyword of the attribute and the value in strings
What steps do you need to take in order to insert a new element into the page?
document.createElement(‘element’) assigned to a new variable, then if need be a text create a new variable with the value being .createTextNode(), and then append that textNode to the element and then append that element to a parent element
What is the textContent property of an element object for?
to add text to an element
Name two ways to set the class attribute of a DOM element.
setAttribute() and the className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
you are able to adjust the format of the app, and we can reuse that function over and over again
and you can you that DOM tree in multiple applications