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?

A

No

25
Q

Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?

A
  • More complicated
  • Stick to one source of truth, always keep data in
    JS instead of the DOM
26
Q

Why is storing information about a program in variables better than only storing it in the DOM?

A

Simplify the source of data, and prevent bugs

27
Q

Does the document.createElement() method insert a new element into the page?

A

No

28
Q

What do you pass as the arguments to the element.setAttribute() method?

A
  • attribute type
  • value

$element.setAttribute(‘id’, ‘home’)

29
Q

How do you add an element as a child to another element?

A

element.appendChild($childElement)

30
Q

What steps do you need to take in order to insert a new element into the page?

A

document. createElement(el)
- save into variable

query DOM for parent element that exists on HTML page
- save into variable

parentEl.appendChild(childEl)

31
Q

What is the textContent property of an element object for?

A

textContent will get and set text to an existing node

32
Q

Name two ways to set the class attribute of a DOM element.

A
  • className
  • setAttribute(‘class’ ‘value’)
  • classList.add()
33
Q

What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?

A
  • reusability

- faster to create it once and have a loop handle it

34
Q

What is the event.target?

A

event.target is the the location of where the event occurred

35
Q

Why is it possible to listen for events on one element that actually happen its descendent elements?

A

An event is carried up through the event-chain due to bubbling

36
Q

What DOM element property tells you what type of element it is?

A

event.target.tagName

37
Q

What does the element.closest() method take as its argument and what does it return?

A

The closest method traverses the element and it’s parent and returns closest ancestor of the selected element

38
Q

How can you remove an element from the DOM?

A

$element.remove()

39
Q

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?

A

Add the event listener to the closest parent element that contains all the clickable DOM elements