DOM Flashcards
- Which “document” is being referred to in the phrase Document Object Model?
The HTML document
- What is the word “object” referring to in the phrase Document Object Model?
- The data type object in JavaScript
What is a DOM tree?
- The DOM tree is the model of the HTML document stored as an object in JavaScript
- Give two examples ofdocumentmethods that retrieve a single element from the DOM.
- Document.querySelector();
* Select element by id
- Give one example of adocumentmethod that retrieves multiple elements from the DOM at once.
- document.querySelectorAll();
- Why might you want to assign the return value of a DOM query to a variable?
- So you can work with an element more than once
- Whatconsolemethod allows you to inspect the properties of a DOM element object?
- console.dir();
- Why would ascript tag need to be placed at the bottom of the HTML content instead of at the top?
- The browser needs to parse all of the elements in the HTML page before the JS code can access them.
- What doesdocument.querySelector()take as its argument and what does it return?
- A string, Css selector and it returns the first matching elements
- What doesdocument.querySelectorAll()take as its argument and what does it return?
- A string, Css selector to select all matching elements and returns a NodeList
Difference between Number.isNaN(); and isNan();
Number.isNaN returns true when the argument is a number and is NaN.
isNaN converts to a number and returns true when the resulting value is NaN
- What is the purpose of events and event handling?
They make our apps do stuff!
- Are all possible parameters required to use a JavaScript method or function?
No
- What method of element objects lets you set up a function to be called when a specific type of event occurs?
.addEventListener
callback function
a function definition being passed in as an argument
- What object is passed into an event listener callback when the event fires?
The target of the object
- What is theevent.target? If you weren’t sure, how would you check?
You can check in your browser/ console.
What is the difference?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())
handleClick());
fires immediately and messes up the function when button clicks.
- What is theclassNameproperty of element objects?
- Gets or sets class attribute on element
- What is thetextContentproperty of element objects?
- Gets and sets text element
- How do you update the text within an element using JavaScript?
- Query select element and assign it using textContent
- Is theeventparameter of an event listener callback always useful?
No
- Why is storing information about a program in variables better than only storing it in the DOM?
- If it wasn’t stored in variables it would need to run back and fourth between html and javascript. Keep your code in javascript.
- 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 theevent.preventDefault()method do?
Stops the default behavior for that event
- What does submitting a form withoutevent.preventDefault()do?
Refreshes the page
- What property of a form element object contains all of the form’s controls.
form.element
- What property of a form control object gets and sets its value?
value property
- Does thedocument.createElement()method insert a new element into the page?
No
- How do you add an element as a child to another element?
appendChild(); append();
- What do you pass as the arguments to theelement.setAttribute()method?
‘attribute’, ‘name’
- What steps do you need to take in order to insert a new element into the page?
query the parent element, create the child, give it text content,
parent.appendChild(child);
- Name two ways to set theclassattribute of a DOM element.
className(); or setAttribut();
- What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
you can reuse it or give it a name to the process.
- What is theevent.target?
The DOM object of the HTML element where the event was triggered
- Why is it possible to listen for events on one element that actually happens to its descendent elements?
Event flow when you hover or click on a link you also click on its parent elements. Event bubbling.
- What does theelement.closest()method take as its argument and what does it return?
- It takes a css selector as a string and returns the closest ancestor or element to the event.
- 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?
- You target the parent element.
- What is the affect of setting an element todisplay: none?
- Hides the element without deleting it, used with javascript.
- What does theelement.matches()method take as an argument and what does it return?
- A css selector as a string and returns a boolean
- How can you retrieve the value of an element’s attribute?
- getAttribute(); it takes an argument of a string and returns the attribute you were searching for
- If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?
- You’d have to query every tab and add event listeners for each one
- If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?
- You’d have to individually set each item