DOM Flashcards
What is the DOM?
a model that represents the HTML document as Javascript object data type.
Javascript Object that is modeling a HTML document’s content
What is a DOM tree?
includes all the configurations(attributes) and contents(text & child elements) of the element
what is querySelector()? What does it take as its argument?
retrieves one element from DOM
(string = CSS selector) = first matching element
searches within any DOM element! NOT just for document object!
ex) any object.querySelector(‘p’) = nested p element inside the any object
querySelectorAll(); What does it take as its argument and what does it return?
retrieve multiple elements from DOM
(string = CSS selector) as argument and returns a node list (not a DOM element just a way to group DOM elements) of all matching DOM elements
Why assign the return value of a DOM query to a variable?
to save time and energy (if calling a element that won’t change multiple times) if element might change in the future, may need to recall
Why would a script tag need to be placed on the bottom of the HTML content instead of at the top?
the HTML elements must exist before creating the DOM tree, since the HTML elements are what JS needs to create the DOM!
What is a dir method? aka directory method?
to see the meticulous details and all the data attached to the DOM element
variable with a $?
ONLY when it holds a DOM element as its value
NOT a value pulled from calling a DOM element aka text from calling a DOM element
Why do we log things to the console when we use dom-events?
to check if the event fired
What is the purpose of events and events handling?
events = things that happen in the browser (usually user driven not always)
event handling = developers way to set up a list of actions that will happen in response to the event
Are all possible parameters required to use a javascript method or function?
No
ex) last parameter in addEventHandler()
and splice()
and more
useCapture?
if not specified = default to false
uses bubbling which uses normal flow of descendants out to ancestors
ex) gives event to all ancestor elements where the element with the event was encased
if specified to true
uses capture which goes in to descendants
ex) gives event to all child element within the element with event
addEventListener() is called off of which object?
any DOM element
What object is passed into an event listener callback when the event fires?
the event object = which has all the data about the event that occurred
given by JavaScript
What is the event.target?
it is the target property of the event object that tells us where (DOM element) the event STARTED!
it is not necessarily the element that I put the event listener ON! Due to bubbling, the event could start inside the child of the DOM element that has the event listener.
What is the className property of element objects?
get or set CLASS NAME not CSS selector value of an element property with the css class name of the DOM element
element.className = ‘newName’
or
element.className; return ‘current class’
What is the textContent property of an element object for?
get or set text content value of an element
property with all the text content of the DOM element
Why is storing info about program in var better than only storing in DOM?
getting info from DOM only to manipulate it with JS and then place it back into the DOM is more work!
storing data somewhere else makes work harder!
STORE DATA that needs JS to manipulate it, IN JS!
event.target
We get direct reference to where the event is starting/fired!
ex) multiple buttons
target is a property of the event object that stores all the information about the DOM element where the event fired from!
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 when user changes value of form control?
input
What event is fired when user clicks the “submit” button within a form element?
submit
submit event listener should always be on the FORM element!
NOT form control element!
What does event.preventDefault() do?
prevents default behavior of an event
What does submitting a form without event.preventDefault() do?
It refreshes/reloads the page with the form’s values in the URL (due to outdated action attribute that without a URL to send the data to, it would reload the page and send the data to the current page, which is simply refreshing the page + reloading means current data is LOST)
reset() = in the same session, therefore data inside the input is still available