JS & the DOM Flashcards

(49 cards)

1
Q

Why do we log things to the console?

A

So you can see what the data looks like; so you can see what’s going on

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

What is a “model”?

A

Relation to DOM (document object model)

-> The DOM is a representation/model/replica of the HTML document, it is not the actual HTML document;

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 DOM represents all the information in HTML document (elements, text, attributes, etc) as JavaScript objects

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

What is a DOM Tree?

A

It is a model of the HTML document created by the browser and stored in memory. The DOM tree is a tree data structure of nodes

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
  1. document.querySelector( )

2. document.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

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

Querying for an element using document.querySelector() method takes time/processing resources because you are traversing the DOM, looking for a node

If you have the node saved in a variable and want to do something to it, you don’t need to query the DOM for it again

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 (and JavaScript objects in general)?

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

the DOM has to fully load before you can access / manipulate it with JavaScript

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 a string) as an argument and returns the first DOM node that matches the selector (or null if no matching node found)

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 (as a string) as argument and returns a static nodelist of nodes that match the selector

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

They let your webpage respond/react to user actions - with events you can listen for stuff and then do stuff in response to it

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

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

A

No. Just because a function is given parameter doesn’t mean it has to do anything with it.

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

What is a callback function?

A

A function passed into another function as an argument. The outer function calls the callback function later when it needs it.

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

What is the event.target?

A

The element that received the event; the element that was interacted with.

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 class attribute of that html element; access or update the class attribute using this property

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

element. className() method or

element. classList.add( ) / element.classList.remove()

21
Q

What is the textContent property of element objects?

A

The text content of the node and its descendents

22
Q

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

A

element.textContent = newTextContent

23
Q

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

A

No. Just because a function takes a parameter doesn’t mean it has to do anything to it.

24
Q

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

A

Don’t want to store information on DOM because it is unreliable; the DOM should not be your “source of truth’, the javascript code should be

25
What event is fired when a user places their cursor in a form control?
focus
26
What event is fired when a user's cursor leaves a form control?
blur
27
What event is fired as a user changes the value of a form control?
input
28
What event is fired when a user clicks the "submit" button within a ?
submit
29
What does the event.preventDefault() method do?
Prevents the browser’s default response to the event
30
What does submitting a form without event.preventDefault() do?
Causes page to reload with form values in url query string aka submit to server
31
What property of a form element object contains all of the form's controls.
elements
32
What property of form a control object gets and sets its value?
value
33
Does the document.createElement() method insert a new element into the page?
No, before it appears on page must be appended to existing object on DOM
34
How do you add an element as a child to another element?
element.appendChild() or element.append()
35
What do you pass as the arguments to the element.setAttribute() method?
1. attributeName | 2. attributeValue
36
What steps do you need to take in order to insert a new element into the page?
1. Create element 2. set attributes and text content 3. Attach it to the DOM by appending it as child of an existing node
37
What is the textContent property of an element object for?
Used to set or access the text content of an element
38
Name two ways to set the class attribute of a DOM element.
1. element.classList.add() | 2. element.className()
39
What are two advantages of defining a function to create something (like the work of creating a DOM tree)?
1. Functions are reusable | 2. functions have name, so they make your code easier to read
40
What is the event.target?
It is the element that an event originated from; the point of interaction
41
Why is it possible to listen for events on one element that actually happen its descendent elements?
Because of event flow, which causes events to ‘bubble up’ the DOM
42
What DOM element property tells you what type of element it is?
tagName
43
What does the element.closest() method take as its argument and what does it return?
Argument: css selector (as a string) Return value: first (ie. closest) ancestor element that matches selector;
44
How can you remove an element from the DOM?
element.remove()
45
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?
Add the event listener to the parent element (aka use event delegation)
46
What is the event.target?
the element the user interacted with
47
What is the affect of setting an element to display: none?
It hides the element (removes element from the element flow);
48
What does the element.matches() method take as an argument and what does it return?
Argument = a selector string Return value = a boolean value indicating if the element the method is called on matches the selector given as the argument
49
How can you retrieve the value of an element's attribute?
Use the element.getAttribute() method