DOMS Flashcards

1
Q

Why do we log things to the console?

A

To debug and verify

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

It refers to the fact that each node is an object.

Node (for our purposes most likely refers to an HTML element)

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

What is a DOM Tree?

A

An element and all of its content

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

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

A

You might need to work with an element multiple times, so the computer doesn’t have to constantly look for that reference to the variable.

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

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

A

dir

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

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

A

Because if you put it on the top, the html code isn’t loaded yet, and you can’t access the elements.

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

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

A

CSS selector

returns the JS object

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

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

A

CSS selector

returns a node list

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

Why do we log things to the console?

A

Debugging and verification

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

When you want to do something when something else occurs.

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

What method of element objects let 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
15
Q

What is a callback function?

A

It is a function used as an argument in another function.

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

It is the object of the event that was used
e.g. function handleDoubleClick(event) {}
You don’t define the event parameter yourself
- JavaScript prepacks it with all the information about the type of event handleDoubleClick is used with addEventListener

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
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 event target is a reference to the object that caused the event to happen.

use the console and look at MDN

The target is where the event directly occurred from.

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

What is the difference between these two snippets of code?
o element.addEventListener(‘click’, handleClick)
o element.addEventListener(‘click’, handleClick())

A

o element.addEventListener(‘click’, handleClick)
- runs when the event happens
o element.addEventListener(‘click’, handleClick())
- runs when the page loads

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

What is the className property of element objects?

A

It is the value of the class attribute.

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

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

A

variableName.className = “new class”

variableName.classList.add(‘newClass’)

variableName.classList.remove(‘removeThisClass’)

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

What is the textContent property of element objects?

A

It gives you access to the text content within an element.

22
Q

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

A

variableName.textContent = new value

23
Q

Is the event parameter of an event listener always useful?

A

no

24
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

25
Q

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

A

It lets you quickly access it whenever you need it.

26
Q

What event is fired when a user places their cursor in a form control?

A

focus

27
Q

What event is fired as a user changes the value of a form control?

A

input

28
Q

What event is fired when a user clicks the “submit” button within a form?

A

submit

29
Q

what does the event.preventDefault() method do?

A

It stops the event from doing what it normally does?

30
Q

What does submitting a form without event.preventDefault() do?

A

The data will be sent somewhere and the page will reload

31
Q

What property of a form element object contains all of the form’s controls

A

Elements

32
Q

What property of form a control object gets and sets its value?

A

.value

33
Q

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

A

no

34
Q

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

A

Append child

35
Q

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

A

attribute name and value

36
Q

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

A

Create element node
Retrieve the element you want to append to the node
Append the element to the node
Add node to DOM tree

37
Q

What is the textContent property of an element object for?

A

Retrieve or change text content in a node

38
Q

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

A

setAttribute()

className

39
Q

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

A

You can reuse the function.

It makes code more readable.

40
Q

What is the event.target

A

It is where the event occurred from

41
Q

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

A

event bubbling

42
Q

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

A

tagName

43
Q

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

A

Selector

returns the closest node that matches the given selector up the ancestor

44
Q

How can you remove an element from the DOM?

A

.remove()

45
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

You put the event on the least specific node (probably the container)

46
Q

What is the event.target?

A

It is the element where the event occurred from

47
Q

What is the effect of setting an element to display: none?

A

It makes the element act as if it doesn’t exist

48
Q

What does the element.matches() method take as an argument and whatdoes it return?

A

selector

boolean

49
Q

How can you retrieve the value of an element’s attribute

A

getAttribute()

50
Q

At what steps of the solution would it be helpful to log things to the console?

A

When you need to see if something is working the way you intend it to