DOM Flashcards

1
Q

Why do we log things to the console?

A

to check that the code is doing what we think it should be doing / for debugging

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

What is a “model”?

A

a functional, simplified 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.

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

to the javascript objects (an object-oriented language)

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

What is a DOM Tree?

A

a central node serving as the trunk, with a number of children nodes branching out from it (it’s branches), which in turn have their own nodes branch out of them until you get to their terminal elements (their leaves). Basically a matrioshka doll of nested parent-child-sibling elements.

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. getElementByClassName();
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

so you can easily access the properties/values o the DOM object, manipulating, adding, transforming or deleting them.

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 that the DOM objects to be constructed can query their value from already existing elements (otherwise, the value null would be stored in the DOM variables)

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 a CSS selector for the target HTML element, and it returns a JavaScript object representing said element, called a DOM object.

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 a CSS selector for all HTML elements that satisfy it, and returns a JavaScript array that represents a list of all those elements called a DOM list (or a DOM node list).

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

Why do we log things to the console?

A

To check if the code is doing what we expect it to do/ check for bugs.

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 are objects that log information of the web-page triggered by an… event. Event handling is to pass an event as the argument for a function that’ll utilize the data stored in the event object to response to said occurance.

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

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

A

element.addEventListener();

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

What is a callback function?

A

a function passed as the argument of another function

18
Q

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

A

The triggering event object

19
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 element targeted by the event;
using console.log(event.target);
using console.dir(event.target);

20
Q

What is the difference between these two snippets of code?

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

A

in the first one, we have the callback function handleClick being passed as an argument for the .addEventListener method;
while in the second one, the function handleClick() is being called, and the return value of that function is being passed as an argument for the .addEventListener method

21
Q

What is the className property of element objects?

A

the string value of the element’s css class attribute;

22
Q

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

A

assign the new class values as a string to $element.className

23
Q

What is the textContent property of element objects?

A

a string value representing the text content of an html element

24
Q

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

A

$element.textContent = ‘new text’;

25
Q

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

A

no

26
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, for sure. There may be a simpler solution with some property of the click event object, but this may be more vulnerable to outside actors (taking values not stored in the javascript of the page)

27
Q

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

A

the document is suseptible to being tampered with via the dev tools, making it more vulnerable than storing data into javascript variables

28
Q

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

A

no

29
Q

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

A

$parentElement.appendChild($childElement);

30
Q

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

A

a string with the name of the attribute to be set, and another string withe the value to be assign to the attribute;

31
Q

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

A

.creatElement() -> .appendChild()

32
Q

What is the textContent property of an element object for?

A

to get and/or set the text content of an html element

33
Q

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

A

.setAttribute(‘class’, ‘class-value’);

.className = ‘class-value’;

34
Q

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

A

so you can reuse the function to do that piece of work some other time/ multiple times

35
Q

What is the event.target?

A

the DOM object representing the HTML element where the event was triggered

36
Q

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

A

because the default event flow is bubbling upwards

37
Q

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

A

$element.tagName;

38
Q

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

A

it takes a string with the syntax of a CSS selector, and returns the first parent node to match the selector;

39
Q

How can you remove an element from the DOM?

A

$element.delete();

40
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 node for the new elements