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
Q

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

A

No

26
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

27
Q

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

A

It is easier to manage variables than having to query the DOM repeatedly.

28
Q

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

A

focus event

29
Q

What event is fired when a user’s cursor leaves a form control?

A

blur event

30
Q

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

A

input event

31
Q

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

A

submit event

32
Q

What does the event.preventDefault() method do?

A

It prevents the page from reloading after the submit event

33
Q

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

A

It would reload the page after the submit event

34
Q

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

A

form element’s elements property

35
Q

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

A

value attribute

36
Q

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

A

Not being able to see which part of the code is creating a possible error

37
Q

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

A

Being able to check for errors, and confirming values are printing as expected while code is being written

38
Q

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

A

No, it just creates the element. appendChild() must be used to insert into the page.

39
Q

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

A

parentChild.appendChild(childElement)

appends the child as the last child of the parentChild

40
Q

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

A

attribute type, attribute value

41
Q

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

A

createElement()
appendChild() to an existing element in the page
append() can also be used (check MDN)

42
Q

What is the textContent property of an element object for?

A

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
Q

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

A

setAttribute(class, )

object.className

44
Q

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

A

A function can be repeated for different object values, and the return value of the function can be easily managed like a variable

45
Q

What is the event.target?

A

the element that was interacted with in the webpage

46
Q

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

A

event bubbling

47
Q

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

A

.tagName

.nodeName is also applicable, but .tagName is needed to retrieve an actual element

48
Q

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

A

It takes a CSS Selector as its argument and returns the node that matches that CSS Selector

49
Q

How can you remove an element from the DOM?

A

querySelector to find the element node

element.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

use addEventListener on the parent element