DOM Flashcards

1
Q

Why do we log things to the console?

A

We log things as a console both to make sure that the main.js file is interacting with the html file and so that we as developers can test and track values (debugging)

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

What is a “model”?

A

A recreation of something (generally not 1:1 recreation)

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

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 JavaScript object data type

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

What is the DOM?

A

A JavaScript object that is modeling the contents of an HTML document that is loaded into the browser

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

What is a DOM Tree?

A

The DOM tree is a list of objects/nodes that branch off of the document node. It is made up of the different elements, attributes, and text nodes in an HTML document. It is an element plus all of its content and all of its configurations.

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

Give two examples of document methods that retrieve a single element from the DOM.

A

document. getElementbyID(‘id’);

document. querySelector(‘css selector’);

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

Give one example of a document method that retrieves multiple elements from the DOM at once.

A

document. querySelectorAll(‘css selector’);
document. getElementsByClassName(‘class’);
document. getElementsByTagName(‘tag name’);

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

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

A

In case you need to re-use that value of a DOM query multiple times

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

What console method allows you to inspect the properties of a DOM element object?

A

console.dir( ); [ dir is short for directory]

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

Why would a tag need to be placed at the bottom of the HTML content instead of at the top?

A

Because we want to load the content of the HTML document first?

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

What does document.querySelector() take as its argument and what does it return?

A

document.querySelector( ); takes a css selector as it’s argument (in a string) and returns the first element that matches the selector

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

What does document.querySelectorAll() take as its argument and what does it return?

A

document.querySelectorAll( ); takes a css selector as it’s argument (in a string) and returns all elements with that css selector (Node List)

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

Why do we log things to the console?

A

We log things as a console both to make sure that the main.js file is interacting with the html file and so that we as developers can test and track values (debugging)

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

What is the purpose of events and event handling?

A

Events are a way to let us know when an event occurs (usually when a user interacts with the page) and event handling is our way of preparing a response when the event occurs

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

Are all possible parameters required to use a JavaScript method or function?

A

No, there are some parameters that are optional

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

What method of element objects lets you set up a function to be called when a specific type of event occurs?

A

Use addEventListener to the object you want to call the event on

18
Q

What is a callback function?

A

A callback function is a function benign passed into another function as an argument

19
Q

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

A

The event object is passed and the data is all the information of when the event was invoked

20
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 value of the element where the event occurred it is not the element that the event listener is applied on
We can check by console logging the target property of the event object
We can either check the code or MDN for more information

21
Q

What is the difference between these two snippets of code?

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

A

The first one is a callback function and the second is a function call with no argument. Because of this it runs the function with nothing but the first one will run with the event as it’s argument

22
Q

What is the className property of element objects?

A
A string of the class attribute of an element
It can get or set the className attribute on an HTML document
23
Q

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

A

You use the .className on the variable storing the DOM information and assign it a string value of the new class name

24
Q

What is the textContent property of element objects?

A

The textContent property is the text content of a node on the DOM
Property that stores any text content that is present in the node

25
Q

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

A

Use the .textContent on the variable storing the DOM information and assign it the new text you want it to have

26
Q

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

A

No, in functions where you don’t want things to vary you do not need the event parameter
But in general we will want to use the event parameter in most cases

27
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

Harder because instead of referencing a variable with the number of clicks in it, we would have to calculate the number of clicks for each spot we want to check the value of the clicks

28
Q

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

A

If we store the information in a variable then the computer does not have to process getting the value multiple times and makes the work more efficient for the computer. For developers it also makes it easier to read by knowing that we are referencing back to an already stored value.

29
Q

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

A

No it does not, it just creates the element. You need to appendChild to insert the element made into the document

30
Q

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

A

With the appendChild method

31
Q

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

A

The attribute you want to set and the value you want to assign

32
Q

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

A

Create the element, append the element as the child of another element

33
Q

What is the textContent property of an element object for?

A

It is used to receive or modify the text content of an element

34
Q

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

A

element. className(‘new class name’);

element. setAttribute(‘class’, ‘new class name’);

35
Q

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

A

The return of the reusable function can be used in other places (different contexts)

36
Q

What is the event.target?

A

The event.target is the target element that the event happens on

37
Q

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

A

Because of event bubbling

38
Q

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

A

event.target.tagName

39
Q

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

A

It takes a css selector and returns the closest ancestor element (or itself) that matches the argument

40
Q

How can you remove an element from the DOM?

A

‘Item you want to remove in a DOM variable’ . 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

Create a click on all the elements and set a conditional that if the tagName is === to the element you want to affect, then make the changes, otherwise do nothing