DOM Flashcards

1
Q

Why do we log things to the console?

A

For debugging and an easy way to inspect your variables in the browser

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

What is a “model”?

A

Replica

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

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

A

The HTML

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

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

A

JavaScript object

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

What is a DOM Tree?

A

DOM element and all of the items in it

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

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

A

getElementById()

querySelector()

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

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

A

querySelectorAll()

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

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

A

For reusability and accessibility

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
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
10
Q

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

A

The browser needs to run the html document first in order to create a DOM

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

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

A

CSS selector and it returns the first element from the document

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

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

A

CSS selector and it returns NodeList of all elements matching the selector

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

What is the purpose of events and event handling?

A

Events are actions or occurrences that happen in the system

Event handling is steps to trigger some JavaScript code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
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
15
Q

What method of element objects lets 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
16
Q

What is a callback function?

A

A callback function is a function passed into another function as an argument

17
Q

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

A

An object with all of the possible data the browser can provide

18
Q

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

A

The target property of the Event interface is a reference to the object onto which the event was dispatched

19
Q

What is the difference between these two snippets of code?

element. addEventListener(‘click’, handleClick)
element. addEventListener(‘click’, handleClick())

A

In the first snippet, the event listener will call on the handleClick function only when the event occurs
In the second snippet, the event listener is calling on the handleClick function but that immediately calls the function

20
Q

What is the className property of element objects?

A

Gets and sets the value of the class attribute of the specified element.

21
Q

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

A

Assign a string with new property name to the className property of element object

22
Q

What is the textContent property of element objects?

A

Represents the text content of the node and its descendants.

23
Q

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

A

Assign a string with new text to the textContent property of element object

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

No, we would need the variable to keep track of number of clicks or we would have to make a new variable every time

26
Q

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

A

For easy reusability and accessibility

27
Q

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

A

It does not

28
Q

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

A

element.appendChild()

29
Q

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

A

The name and the value of the attribute

30
Q

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

A

Create the element, give it content, add it to the dom

31
Q

What is the textContent property of an element object for?

A

Represents the text content of the node and its descendants.

32
Q

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

A

element. setAttribute()

element. className

33
Q

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

A

Saves time in the long rung

Easy to reuse

34
Q

What is the event.target?

A

The target of the event (most specific element interacted with)

35
Q

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

A

Because of event bubbling
When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.

36
Q

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

A

.tagName

37
Q

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

A

Takes a DOMString containing a selector list

Returns the element which is the 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 DOM element inside the parent node containing the other DOM elements