DOM Flashcards
What is a “model”?
A representation of an object/something
Which “document” is being referred to in the phrase Document Object Model?
The HTML document for that web application
What is the word “object” referring to in the phrase Document Object Model?
The model created is made out of objects in the JavaScript language.
What is a DOM Tree?
Each branch of the tree ends in a node, and each node contains objects.
The Document Object Model (DOM) connects web pages to scripts or programming languages by representing the structure of a document in memory.
A model of the web page and stored in browsers’ memory. / Element and all its children, and any data in the folder??
Give two examples of ‘document’ methods that retrieve a single element from the DOM.
‘getElementById()’ and ‘querySelector()’
Give one example of a ‘document’ method that retrieves multiple elements from the DOM at once.
‘getElementsByClassName()’ or ‘getElementsByTagName()’ or ‘querySelectorAll()’
Why might you want to assign the return value of a DOM query to a variable?
Store location of element and don’t have to look for it again
So you can access and work on the same element node more than once.
What ‘console’ method allows you to inspect the properties of a DOM element object?
dir
‘console.dir()’
Why would a ‘script’ tag need to be placed at the bottom of the HTML content instead of at the top?
The elements need to load first and when try to access through javascript, the elements exist first
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?
Takes CSS selector as argument and returns the first matching element.
What does ‘document.querySelectorAll()’ take as its argument and what does it return?
Takes CSS selector as argument and returns a NodeList of all elements with argument.
What is the purpose of events and event handling?
Events are things that happen; event handlers are functions we provide, work that should happen when events happen; so developers can respond to visitors actions
What do [] square brackets mean in function and method syntax documentation?
Means optional
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?
Event object which contains all information about the event
What is the ‘event.target’? If you weren’t sure, how would you check? Where could you get more information about it?
The target property of the Event interface is a reference to the object onto which the event was dispatched. (property where the event originates from?)
MDN
What is the difference between these two snippets of code?
‘element.addEventListener(‘click’, handleClick)’
‘element.addEventListener(‘click’, handleClick())’
The second will call the function handleClick when the page loads. The first will call the function when the event fires.
2nd: function is going to be run immediately; even before event
What is the ‘className’ property of element objects?
Property holding class attribute of element
The className property of the ‘Element’ interface gets and sets the value of the class attribute of the specified element.
How do you update the CSS class attribute of an element using JavaScript?
object.className = ‘string’
var variable-name = document.querySelector(‘class-name’);
variable-name.className = ‘new-class-name’;
What is the ‘textContent’ property of element objects?
Text in containing element and its children
The textContent property of the ‘Node’ interface represents the text content of the node and its descendants.
How do you update the text within an element using JavaScript?
object.textContent = ‘newvalue’
var variable-name = document.querySelector(‘class-name’);
variable-name.textContent = ‘new strings’
Is the ‘event’ parameter of an event listener callback always useful?
Not always you don’t always need information about event — function(EVENT)
Why is storing information about a program in variables better than only storing it in the DOM?
It is more work for the browser to not use a variable