DOM Flashcards

1
Q

Why do we log things to the console?

A

To be aware of what our code Is doing and to make sure it has the outcome we want

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

What is a “model”?

A

A replication of a dom tree that is stored in the browser’s terminal

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 document we are working on

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

Each object refers to a different part of the page in the browser

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

What is a DOM Tree?

A

Collection of all the objects that make up the document

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

Document.getElementById() & document.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

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

If you know that you need to call on that query multiple times

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

So it can run the entire body to map out a tree before running our script

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 our class or id as an argument and returns the first instance of it

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 all the elements or class we are looking for and returns a nodes list

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

What are some differences of Nodes list and HTML collection?

A

o Html collection will live update as you add things
o Nodes list is static and would not update

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

Events will trigger code. Event handling is to run blocks of code based on what happens on our webpage

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

What method of element objects let 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 function passed into another function as an argument which is then invoked inside the outer function to complete some kind of action

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

The event object. It gives us information about the event that actually occurs

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 specific html element that was interacted with and that caused it to occur

19
Q

What is the difference between these two snippets of code? element.addEventListener(‘click, handleClick) & element.addEventListener(‘click’, handleClick())

A

a function without the parenthesis is a reference to the function. With parenthesis you are actually calling it.

20
Q

What is the className property of element objects?

A

the current classes on the object and allows us to dynamically update it

21
Q

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

A

By using .className = and then the name you want to change it to

22
Q

What is the textContent property of element objects?

A

It represents the text content of the node and its descendants

23
Q

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

A

Calling that element.textContent = the new value you want to assign to that element

24
Q

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

A

It is useful but sometimes you do not need it, sometimes it is useful to know what the function is expecting in its parameter though

24
Q

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

A

It is useful but sometimes you do not need it, sometimes it is useful to know what the function is expecting in its parameter though

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 because we would have to query the dom everytime we needed to use an element. Instead we can just store the results so it does not have to query again

26
Q

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

A

Because the variable will always be able to find where that information is without having to go through the whole html file first
The dom will be dynamic and constantly be changing things

27
Q

What happens if you do not give .className a value?

A

it will just tell you what the class is doing

28
Q

Why is innerHTML bad?

A

Puts exposure to hacking

29
Q

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

A

No

30
Q

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

A

appendChild() method

31
Q

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

A

Name, value

32
Q

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

A

Create it and then append it to something

33
Q

What is the textContent property of an element object for?

A

You can get textContent and use it to set textContent

34
Q

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

A

setAttribute & className

35
Q

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

A

Main advantage is that it is reusable

36
Q

What is the event.target?

A

o it is a reference to the object which the event is occuring to
o whatever element that was actually interacted with

37
Q

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

A

Happens on the child and bubbles upward

38
Q

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

A

o .tagName
o It will tell you which element you are actually interacting with if you use a ‘click’ event

39
Q

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

A

o This method will go from the current element to whichever id, class, or element you put in as an argument
o Css selector and will return the first one that matches it

40
Q

How can you remove an element from the DOM?

A

Item you want to remove .remove()

41
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 parent so that you will capture everything that happens to its children