DOM Flashcards

1
Q

Why do we log things to the console?

A

so we can check our code for bugs along the way and make sure everything is running without error

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

What is a “model”?

A

something you want to mirror or follow

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

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

A

So you can store the reference or location of the element you’re trying to retrieve into the variable so the browser doesn’t have to keep going back to look for the element

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

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

A

javascript objects

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

What is a DOM Tree?

A

A model of the page when it loads in a browser in the form of a tree.

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

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

A

the content of the HTML page need to be set first before the dom tree is made

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

takes in a css selector and returns the first element that matches

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

takes in a css selector and returns a NodeList

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

add dynamic content and interactivity. respond to occurrences.

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

function definition being passed around as a value so it can be invoked later

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

place where the event actually occurred.

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

top one is just a callback function that’s being passed around as a value. the second one you’re calling the function right then and there

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

get element from DOM, use className property with assignment operator, and new value

22
Q

What is the textContent property of element objects?

A

text inside of the element

23
Q

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

A

get element from DOM and assignment operator with textContent

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

26
Q

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

A

If you wanna do work in JS not very intuitive to go into html and bring it back to do something. Want everything to be in one spot

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

appendChild

29
Q

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

A

attribute 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

1) querySelector element to be appended to
2) create element
3) append element

31
Q

What is the textContent property of an element object for?

A

property that holds the text content of the element

32
Q

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

A

setAttribute, className

33
Q

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

A

reusability

34
Q

What is the event.target

A

object that stores the element where the event started

35
Q

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

A

event bubbling - starts from element where it began and goes up

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

css selector and returns itself or matching ancestor

38
Q

How can you remove an element from the DOM?

A

remove(). remove() needs to be applied to the specific element you’re trying to 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

put eventlistener on just the parent

40
Q

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

A

completely hides the element

41
Q

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

A

selector string 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

every step

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

we’d have to add an event listener to each element

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

you’d need to write a conditional for each view.