DOM Flashcards

1
Q

Dom Querying
Why do we log things to the console?

A

to make sure the script is loading properly, to check for bugs, make sure everything is working smoothly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Dom Querying
What is a ‘model’?

A

something recreating (representing) something else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Dom Querying
Which ‘document’ is being referred to in the phrase Document Object Model?

A

html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Dom Querying
What is the word ‘object’ referring to in the phrase Document Object Model?

A

it’s referring to different parts of the page loaded in the browser window

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Dom Querying
What is a DOM Tree?

A

an element and all of its configurations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Dom Querying
Give two examples of document methods that retrieve a single element from the DOM

A

getElementById(‘id’)
querySelector(‘css selector’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Dom Querying
Give one example of a document method that retrieves multiple elements from the DOM at once

A

querySelectorAll(‘css selector’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Dom Querying
Why might you want to assign the return value of a DOM query to a variable?

A

make it easier to use, otherwise you have to write it out every time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Dom Querying
What console method allows you to inspect the properties of a DOM element object?

A

console.dir()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Dom Querying
Why would a

 tag need to be placed at the bottom of the HTML content instead of at the top?
A

because the browser needs to parse all the elements in the HTML page before Javascript code can access them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Dom Querying
What does document.querySelector() take as its argument and what does it return?

A

a string containing one or more css selectors
returns: first element that matches the specified selector or group of selectors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Dom Querying
What does document.querySelectorAll() take as its argument and what does it return?

A

a string containing one or more selectors
returns a static (not live) nodelist containing one element object for each element that matches at least one of the specified selectors or an empty nodelist (not an array) if there isn’t a match

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

DOM Events:
Why do we log things to the console?

A

for this exercise, it was to see when the event fires
for debugging
make sure that script is loading properly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

DOM Events:
What is the purpose of events and event handling?

A

so that users can interact with the web page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

DOM Events:
Are all possible parameters required to use a JavaScript method or function?

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

DOM Events:
What method of element objects let you set up a function to be called when a specific type of event occurs?

A

.addEventListener()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

DOM Events:
what is a callback function?

A

it is a function that is passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action

18
Q

DOM Events:
What object is passed into an event listener callback when the event fires?

A

the event object
(it contains information about the event like event type, target element, and the time the event occurred)

19
Q

DOM Events:
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?

A

the element where the event occurred
you can console.log if you’re not sure
mdn for more info

20
Q

DOM Events:
what is the difference between these two snippets of code?
element.addEventListener(‘click’, handleClick)
element.addEventListener(‘click’, handleClick())

A

the first is being passed as a callback function
second gets called immediately and doesn’t get to be an event handler, gets replaced by its return

21
Q

DOM Manipulation:
What is the className property of element objects?

A

it gets and sets the value of a specific class attribute of a specific element
element.className = ‘attribute’

22
Q

DOM Manipulation:
how do you update the CSS class attribute of an element using JavaScript?

A

element.className = ‘classNames’

23
Q

DOM Manipulation:
What is the textContent property of element objects?

A

allows you to update text content

24
Q

DOM Manipulation:
How do you update the text within an element using JavaScript?

A

node.textContent = ‘text content’

25
DOM Manipulation: Is the event parameter of an event listener callback always useful?
no
26
DOM Manipulation: 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
27
DOM Manipulation: Why is storing information about a program in variables better than only storing it in the DOM?
so that you can access and change the information easily?
28
DOM Creation: Does the document.createElement() method insert a new element into the page?
no
29
DOM Creation: How do you add an element as a child to another element?
.appendChild() parentElement.appendChild(childElement)
30
DOM Creation: What do you pass as the arguments to the element.setAttribute() method?
name of attribute value you want to assign to that attribute
31
DOM Creation: What steps do you need to take in order to insert a new element into the page?
1. create the element, use .createElement() var newEl = document.createElement('element') 2a. give it content, use .createTextNode() or .textContent to give it text - var name = document.createTextNode('text content') - newEl.textContent = variable or 'textContent' 2b. and/or .setAttribute() to give it an attribute and attribute value newEl.setAttribute('attributeName', 'attributeValue') 3. add it to the DOM, use .appendChild() variableForParentElement.appendChild(varForChildElement)
32
DOM Creation: What is the textContent property of an element object for?
it represents the text content of the element (node) - use it to assign new textContent to an element
33
DOM Creation: Name two ways to set the class attribute of a DOM element.
Element.setAttribute('name', 'value') element.className = 'value'
34
DOM Creation: What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
you can use it again in the future you don't have to type everything out again when you're trying to create something
35
DOM Event Delegation: What is the event.target?
it is the element the event interacted with
36
DOM Event Delegation: Why is is possible to listen for events on one element that actually happen on its descendent elements?
because of bubbling
37
DOM Event Delegation: What DOM element property tells you what type of element it is?
event.target tagName property?
38
DOM Event Delegation: What does the element.closest() method take as its argument and what does it return?
it takes a CSS selector and returns the closest ancestor (parent) element, itself, or null (opposite of querySelector)
39
DOM Event Delegation: How can you remove an element from the DOM?
the remove() method element.remove() takes no arguments
40
DOM Event Delegation: 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?
add the event listener to the parent element