DOM Flashcards

1
Q

Why do we log things to the console?

A

To verify that the result is what we’re expecting

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

What is a “model”?

A

A representation/recreation of the actual thing. In this case, a DOM tree

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/web page

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

Data representation of all the objects in the document. Any of the element and all of its child and content

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

querySelector(), getElementById()

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(), getElementsByClassName(), getElementsByTagName()

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

If you want to reuse the query

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 parse all of the elements in the HTML page before JavaScript can access them

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

It will take a CSS selector and returns the first matching element

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

It will take a CSS selector and returns all matching elements in a node list

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

Why do we log things to the console?

A

To verify that the result is what we’re expecting

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

So our script can respond to certain events/user inputs

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, having a parameter for a function doesn’t mean that it will be used

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

What is a callback function? Function passed into another function as an argument to be used later

17
Q

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

A

Event object - function as the call back function

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 returns the element that triggered the event. Check on MDN. The element where our target originated from (not attached)

19
Q

What is the difference between these two snippets of code?

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

A

first one is a variable, second is a function being called. Event handler is the function run when an event listener is run.

20
Q

What is the className property of element objects?

A

It’s used to update or retrieve the class name on an element

21
Q

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

A

Get the element and assign the new class name property

22
Q

What is the textContent property of element objects?

A

To add or update text within a specific element

23
Q

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

A

Using textcontent property on the DOM property and assigning it the new text content

24
Q

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

A

No, it’s not always necessary. The event object was not used.

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

It would be more complicated

26
Q

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

A

We don’t want to have to look elsewhere for the information. Keep the JavaScript data in JavaScript.

27
Q

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

A

No

28
Q

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

A

anotherElement.appendChild(element)

29
Q

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

A

(‘attribute name’, ‘value’)

30
Q

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

A

Create new element stored in a variable (createElement), create new text stored in a variable (createTextNode), attach text node to element with appendChild, find the position where the new element stored in a variable, insert new element into its position with appendChild().

31
Q

What is the textContent property of an element object for?

A

Setting or returning the text content of specified node and all its descendants

32
Q

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

A

className or setAttribute

33
Q

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

A

Use it in a addEventListener, use the function over again

34
Q

What is the event.target?

A

The target property returns the element that triggered the event

35
Q

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

A

Because of event bubbling. Event is being carried forward to each ancestor of the chain

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

It takes a string CSS selector and returns closest ancestor of it

38
Q

How can you remove an element from the DOM?

A

elementToRemove.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

By delegating the event to the parent element that contains all the clickable elements