DOM Flashcards

1
Q

Why do we log things to the console?

A

to spot check that our code works and debug it, verification, landmark to see code execute.

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

What is a “model”?

A

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

the HTML document. the entire 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

refers to the data type object within javascript.

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

What is a DOM Tree?

A

an element plus all of its children and their configuration.

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

to store the reference/location of the object in the DOM tree (caching). it saves the browser looking through the DOM tree to find the same element again.

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(object)

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

it speeds up page load times by loading the HTML content first before running DOM scripts.

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 takes CSS selectors as a string as its argument 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 takes CSS selectors as a string as its argument and returns a nodelist of all the elements.

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

to prepare to trigger a function and make the website more interactive

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, there are optional parameters.

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, which is then invoked inside the outer function to complete some kind of routine or 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

information about what happens.

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

it holds the element where the information originated from. you can check with console log and find more information on 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

the parentheses are omitted. parentheses 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

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

element.className = ‘new class name’

22
Q

What is the textContent property of element objects?

A

allows you to collect or update just the text in its containing element

23
Q

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

A

element.textContent = ‘new text’

24
Q

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

25
Would this assignment be simpler or more complicated if we didn't use a variable to keep track of the number of clicks?
More complicated
26
Why is storing information about a program in variables better than only storing it in the DOM?
By keeping data in javascript you don't make your data dependent on another language in the DOM.
27
Does the document.createElement() method insert a new element into the page?
No. it is stored in a variable until attached to DOM tree later with appendChild.
28
How do you add an element as a child to another element?
appendChild()
29
What do you pass as the arguments to the element.setAttribute() method?
name (of attribute) and value
30
What steps do you need to take in order to insert a new element into the page?
create element, store in a variable, DOM query, appendChild
31
What is the textContent property of an element object for?
it represents the text content of the node and its descendants
32
Name two ways to set the class attribute of a DOM element.
setAttribute() and .className
33
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
You can reuse it later on without having to create a new DOM tree. you can give it the function a name so it's easier to track.
34
What is the event.target?
it holds the element where the information originated from
35
Why is it possible to listen for events on one element that actually happen its descendent elements?
Event bubbling flow.
36
What DOM element property tells you what type of element it is?
event.target.tagName ... tagName is in all caps.
37
What does the element.closest() method take as its argument and what does it return?
selectors - a string of a valid CSS selector returns the closest ancestor element or itself, which matches the selectors. If none, returns null.
38
How can you remove an element from the DOM?
element.remove()
39
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?
By adding the event listener to an ancestor element.
40
What is the affect of setting an element to display: none?
it removes the element from the document flow.
41
What does the element.matches() method take as an argument and what does it return?
selectors (a string containing valid CSS selectors) to test against element returns true if element matches selector, false otherwise
42
How can you retrieve the value of an element's attribute?
getAttribute()
43
At what steps of the solution would it be helpful to log things to the console?
every step
44
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?
It would have to have a function and event listener for each tab and view
45
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?
you would have to write a function and event listener for each tab and view and have a conditional statement for each function to hide the other tabs.