DOM Flashcards

1
Q

Why do we log things to the console?

A

To see what is going on, for debugging purposes

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 webpage itself

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 HTML page

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

What is a DOM Tree?

A

The pathways of elements and their children

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

querySelector

getElementById

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 prevent repeated queries to the DOM, just reuse the variable

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

To allow the HTML code to be parsed first, then JavaScript can read them
(The elements need to be put on the DOM first, then moved around with JS)

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

CSS selector as argument, and returns the location of the element selected

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

CSS selector as argument, and returns a 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

For debugging, check results of javascript code

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

Listening for user interactions with the page

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

addEventListener

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

What is a callback function?

A

When a function is passed into anothe function or method

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

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

A

The event object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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 at the point of interaction in the DOM

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

What is the difference between these two snippets of code?

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

A

1st one sets handleClick as a callback function. 2nd one calls the function within the arguments, thus passing the return value as the second argument.

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

What is the className property of element objects?

A

A property that receives and assigns the class attribute of the target element

22
Q

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

A

object.classname = ‘updated class attribute’;

23
Q

What is the textContent property of element objects?

A

It represents the text content of a node and its descendants

24
Q

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

A

object.textContent = ‘New text content’;

25
Is the event parameter of an event listener callback always useful?
No
26
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
27
Why is storing information about a program in variables better than only storing it in the DOM?
It is easier to manage variables than having to query the DOM repeatedly.
28
What event is fired when a user places their cursor in a form control?
focus event
29
What event is fired when a user's cursor leaves a form control?
blur event
30
What event is fired as a user changes the value of a form control?
input event
31
What event is fired when a user clicks the "submit" button within a ?
submit event
32
What does the event.preventDefault() method do?
It prevents the page from reloading after the submit event
33
What does submitting a form without event.preventDefault() do?
It would reload the page after the submit event
34
What property of a form element object contains all of the form's controls.
form element's elements property
35
What property of a form control object gets and sets its value?
value attribute
36
What is one risk of writing a lot of code without checking to see if it works so far?
Not being able to see which part of the code is creating a possible error
37
What is an advantage of having your console open when writing a JavaScript program?
Being able to check for errors, and confirming values are printing as expected while code is being written
38
Does the document.createElement() method insert a new element into the page?
No, it just creates the element. appendChild() must be used to insert into the page.
39
How do you add an element as a child to another element?
parentChild.appendChild(childElement) | appends the child as the last child of the parentChild
40
What do you pass as the arguments to the element.setAttribute() method?
attribute type, attribute value
41
What steps do you need to take in order to insert a new element into the page?
createElement() appendChild() to an existing element in the page append() can also be used (check MDN)
42
What is the textContent property of an element object for?
To set or change the text content of an element text content can be created and assigned to a new element using textContent does destroy other child elements that currently exists in the designated element
43
Name two ways to set the class attribute of a DOM element.
setAttribute(class, ) | object.className
44
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
A function can be repeated for different object values, and the return value of the function can be easily managed like a variable
45
What is the event.target?
the element that was interacted with in the webpage
46
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling
47
What DOM element property tells you what type of element it is?
.tagName | .nodeName is also applicable, but .tagName is needed to retrieve an actual element
48
What does the element.closest() method take as its argument and what does it return?
It takes a CSS Selector as its argument and returns the node that matches that CSS Selector
49
How can you remove an element from the DOM?
querySelector to find the element node | element.remove()
50
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?
use addEventListener on the parent element