DOM Flashcards

1
Q

What is a “model”?

A

A representation or recreation of an original

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

Which “document” is being referred to in the phrase Document Object Model?

A

The HTML document

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

What is the word “object” referring to in the phrase Document Object Model?

A

Refers to a JavaScript object

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

What is a DOM Tree?

A

The DOM tree is a model of a web page

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

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

A
  • getElementById()

- querySelector() – only use querySelector

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

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

A
  • getElementsByClassName()
  • getElementsByTagName()
  • querySelectorAll()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

To save the browser from needing to look up the same elements again if it is intended to be reused.

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

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
9
Q

Why would a script tag need to be placed at the bottom of the HTML content instead of at the top?

A

So the html content can load first

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

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

A
  • String argument is a CSS Selector (type, class id)

- Returns the first matching element

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

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

A
  • String argument is a CSS Selector (type, class id)

- Returns all matching elements in a node list

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

How do you query for attributes?

A

(element + attrtype + attrname)

document.querySelector(‘p#explanation’)

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

What is the DOM?

A

Representation of a JavaScript object describing the html document

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

What is the purpose of events and event handling?

A

Enable functionality with events on the webpage

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

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

What method of element objects lets you set up a function to be called when a specific type of event occurs?

A

addEventListener() method

17
Q

What is a callback function?

A

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

18
Q

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

A

Event object

19
Q

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

A

A reference to the object onto which the event was dispatched

Learn more with:
console.dir(event.target)

20
Q

What is the className property of element objects?

A

It gets and sets class to an element

21
Q

How do you update the CSS class attribute of an element using JavaScript?

A

$element.className = “classone classtwo“

22
Q

What is the textContent property of element objects?

A

textContent property allows you to get and set the text inside a node

23
Q

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

A

node.textContent = “Assign text value”

24
Q

Is the event parameter of an event listener callback always useful?

25
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 - Stick to one source of truth, always keep data in JS instead of the DOM
26
Why is storing information about a program in variables better than only storing it in the DOM?
Simplify the source of data, and prevent bugs
27
Does the document.createElement() method insert a new element into the page?
No
28
What do you pass as the arguments to the element.setAttribute() method?
- attribute type - value $element.setAttribute('id', 'home')
29
How do you add an element as a child to another element?
element.appendChild($childElement)
30
What steps do you need to take in order to insert a new element into the page?
document. createElement(el) - save into variable query DOM for parent element that exists on HTML page - save into variable parentEl.appendChild(childEl)
31
What is the textContent property of an element object for?
textContent will get and set text to an existing node
32
Name two ways to set the class attribute of a DOM element.
- className - setAttribute(‘class’ ‘value’) - classList.add()
33
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
- reusability | - faster to create it once and have a loop handle it
34
What is the event.target?
event.target is the the location of where the event occurred
35
Why is it possible to listen for events on one element that actually happen its descendent elements?
An event is carried up through the event-chain due to bubbling
36
What DOM element property tells you what type of element it is?
event.target.tagName
37
What does the element.closest() method take as its argument and what does it return?
The closest method traverses the element and it's parent and returns closest ancestor of the selected element
38
How can you remove an element from the DOM?
$element.remove()
39
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 closest parent element that contains all the clickable DOM elements