DOM Flashcards

1
Q

Why do we log things to the console?

A

To test we have the correct data and keep track of where we are, in regards to the script

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 an original thing

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 objects within the document

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

What is a DOM Tree?

A

The representation of the HTML with all the children and their descendants within the 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. queryselector(“CSS selector”);

document. getElementByID(“ID name”);

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(“CSS selector”);

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 we need the item repeatedly, we can store the value to help reduce the work it needs to do.

Querying an object takes a good amount of work for your computer

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

The elements need to have loaded into the page before they are manipulated by our script

Most scripts (HTML, CSS, JS) are read from top to bottom.

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 as its argument and returns the while item w/ its content

<h1>Title<h1>

It returns the first item matching that description, from top down</h1></h1>

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

A CSS selector and it returns a list of all the nodes that meet the specification

It returns an list of all the nodes we asked, and all their additional details from .dir

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

Events allow us to determine when something occurs, like a user interacting with our page or something in the HTML changing with DOM

Handling the events would mean we have a response to the event we are now listening for. In the form of a function, with whatever commands we need

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

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

A

.addEventListener

element.addEventListener(‘event’, functionName);

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

What is a callback function?

A

When we use a function as an argument for another function

function getFullName (first, last) {
	var fullName = first + ' ' + last;
	return fullName;
}
function greet(getFullName('John', 'Doe')) {
	console.log('Welcome to the program ' + getFullName)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

An event object, which is being created by the browser, for you.

It is an object which contains all the information needed

17
Q

What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?

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

A

Event target is the object we are watching, usually in the form of an element

We can check by consoling the event and the event.target

The line without the () after the function would work. The other would not. () would run instantly, when the code is read, which is not what we want.

18
Q

What is the className property of element objects?

A

It allows you to replace the class of a stored element

element.className = ‘new-classes’;

19
Q

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

A

element.className = ‘new-classes’;

20
Q

What is the textContent property of element objects?

A

It allows you to replace all the content (including children) of a stored element, with your new text

21
Q

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

A

element.textContent = ‘This is the new text’;

22
Q

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

A

For the event listener, not always, but it will always be there globally

In some cases, no arguments need to be passed into the listener

23
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 difficult. Instead of using a counter, we would need to ask the DOM for the value of the elements, every time we were going to interact with them.

24
Q

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

A

It allows for the selected element to be called multiple times, without issues.

25
Q

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

A

No. It created an element but it is not added until we append it.

26
Q

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

A

parentVar.appendChild(childVar);

27
Q

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

A

(‘attributeType’, ‘valueOfAttribute’);

element.setAttribute(‘src’,’image/dog.jpeg’);

28
Q

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

A

Create and store a created element in the DOM

Get the parent element from the DOM

Append the child into the parent element

29
Q

What is the textContent property of an element object for?

A

It allows you to modify the text of a stored element you got from the DOM

$h1.textContent = ‘Yo mama!’

30
Q

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

A

element. className = ‘large-font’;

element. setAttribute(‘class’, ‘large-font’);

31
Q

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

A

Saving the item saves the computer work by only asking it to pull from the document one time, as opposed to each time we need to edit it

It facilitated how we can call the element, for later use

32
Q

What is the event.target?

A

The target is the entire thing that was specifically clicked.
If your whole li is listening for a click, and it has a span in it, you’ll get the following:
<span>Text Content</span>

33
Q

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

A

When the event occurs, we can see the list of items within the parent, to specifically know what part of the node was clicked.

34
Q

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

A

event.target.tagName will tell you the element/tag, in all capital letters

For the span example, it’ll return the string “SPAN”

35
Q

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

A

It takes the target you are currently selecting and selects the closest item you are searching for, FROM ANCESTORS

For example:
You clicked on the a tag in this string: «a>div</a>> Find Store «a>a</a>>text>
event.target: «a>a</a>>text
You want to store the whole thing containing the a tag:
var container = event.target.closest(‘div’);

36
Q

How can you remove an element from the DOM?

A

$h1.remove();

37
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 entire section, as opposed to the specific button

From there, you can use event.target to observe which items are being interacted with.