DOM Flashcards

1
Q

Why do we log things to the console?

A

To check and make sure our output is correct and 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 representation or recreation of an object/item

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

Data type objects in JavaScript

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

What is a DOM Tree?

A

An element with all its configuration

The giant DOM tree that contains multiple DOM trees within it

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

Only use querySelector as getElementByID is the old way

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

Only one you need to use for selecting all elements

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 store for later use

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

All the elements load before the script so you will get null if you put the file above

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

The CSS selector syntax that selects all elements and returns only the first one that matches it

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

The CSS selector syntax and returns all the elements that match it

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

Event listener listens for the events and handling calls the functions that relate 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

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

addEventListener

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

What is a callback function?

A

Passing function as an argument to another function

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

An object filled with properties that describe the event

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

What is the event.target?

MDN

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

What is the difference between these two snippets of code?

element.addEventListener(‘click’, handleClick)

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

A

First one is passing the function as a value and the second one is calling the function right now

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

What is the className property of element objects?

A

Gets the current set of class elements and sets the value of the class element

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

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

A

element.className =

22
Q

What is the textContent property of element objects?

A

Get or set the current value of the text element

23
Q

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

A

element.textContent =

24
Q

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

A

No

25
Q

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

A

Storing it in variables allows you to reuse again compared to finding it in the DOM and having to convert 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 when a user’s cursor leaves a form control?

A

Blur

28
Q

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

A

Input

29
Q

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

A

Submit

Only fires on the form element itself, not the submit button (never put a click event listener on the button)

30
Q

What does the event.preventDefault() method do?

A

Prevents the browser from automatically reloading the page with the form’s values in the URL

31
Q

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

A

It will refresh the page with the forms data in the URL

32
Q

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

A

form.elements

33
Q

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

A

.value

34
Q

What is one risk of writing a lot of code without checking to see if it works so far?

A

You can have bugs in the code and not know about it

35
Q

What is an advantage of having your console open when writing a JavaScript program?

A

You can see bugs appear right away

36
Q

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

A

No, it only creates it

37
Q

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

A

document.appendChild()

38
Q

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

A

Name and value

39
Q

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

A

Create element and assign to variable, set class name/attribute, optional text node, query for the parent element, and append as child to parent element

40
Q

What is the textContent property of an element object for?

A

For assigning text to an element or see what the current text of the element is

41
Q

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

A

Using document.className method or document.setAttribute

42
Q

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

A

You only have to create it once and it can be reused over and over

Don’t have to retype it over again

43
Q

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

A

It removes the element from the page

Element is no longer part of the document flow

44
Q

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

A

A CSS selector and returns a boolean on whether that element has that selected selector

45
Q

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

A

getAttribute()

Takes the name of the attribute as an argument

46
Q

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

A

After each line of code

47
Q

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

A

Bubbling

48
Q

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

A

A CSS selector the form of a string and returns the closest ancestor/parent element that matches it

Returns null if nothing matches it

49
Q

How can you remove an element from the DOM?

A

.remove()

50
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