DOM Flashcards

1
Q

Why do we log things to the console?

A

to check the output and help debug

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

What is a “model”?

A

representation of something

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

HTML document

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

the objects that comprise the structure and content of a document on the web

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

What is a DOM Tree?

A

Every element, attribute, and piece of text in the HTML is represented by its own DOM node, which makes up the DOM tree

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 and 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

To save the location; saves the browser from having to look for it every time.

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

HTML document needs to loads first so javascript can manipulate 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 string and returns only the first of the matching elements

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 string and returns all elements that match

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 inform us of user interaction and event handling instructs how to respond to the event

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

What do [] square brackets mean in function and method syntax documentation?

A

denotes optional

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

What is a callback function?

A

a function passed into another function as an argument

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

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

A

event?

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

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

A

event object

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

target property of event; mdn

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

What is the difference between these two snippets of code?

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

A

presence of parentheses;
the parentheses are omitted where the function is called because they would indicate that the function should run as the page loads (rather than when the event fires)

20
Q

What is the className property of element objects?

A

DOM property that 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

className property

22
Q

What is the textContent property of element objects?

A

property that represents the text of the node and its descendants; allows text to be collected/updated

23
Q

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

A

textContent property

24
Q

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

A

no, because it is not always needed

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 difficult

26
Q

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

A

to avoid having to search for the information every time that it is needed

27
Q

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

A

no, simply creates an element that can be added to the DOM tree

28
Q

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

A

using appendChild()

29
Q

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

A

string attribute name and attribute value

30
Q

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

A

createElement(), pick the spot you want to place it in, appendChild()

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

.className and setAttribute()

33
Q

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

A

reusable, saves time, and minimizes errors?

34
Q

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

A

event bubbling from most specific to least specific

35
Q

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

A

tagName property

36
Q

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

A

takes a string CSS selector as an argument and returns the closest element (which can be itself)

37
Q

How can you remove an element from the DOM?

A

.remove()

38
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

addEventListener to parent rather than the individual child nodes AKA event delegation

39
Q

What is the event.target?

A

a reference to the object onto which the event was dispatched

40
Q

What is the affect of setting an element to display: none?

A

Turns off the display of an element so that it has no effect on layout

41
Q

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

A

takes a css string selector as the argument and returns a boolean

42
Q

How can you retrieve the value of an element’s attribute?

A

.getAttribute()

43
Q

At what steps of the solution would it be helpful to log things to the console?

A

after variable declarations

44
Q

If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?

A

add individual addEventListeners to each child node

45
Q

If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?

A

a lot of individual conditionals