DOM Flashcards

1
Q

Why do we log things to the console?

A

to verify code and get information about code

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

What is a “model”?

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

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

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

What is a DOM Tree?

A

element + content and configuration

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

getElementByID or querySelector

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

getElementsByClassName, getElementsByTagName, 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 reuse in the future (stores location of DOM)

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

load the content of all the elements, documents load from top to bottom

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 syntax (string), returns only the first element that matches

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 (string), returns NodeList

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 an event “fires” , it can be used to trigger a particular function or code

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, not all parameters are required

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

A callback function is a function passed into another function as an argument

passing the definition of the function, not the return

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

event object that has data about the event that occured

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

event.target is the target element being used / originated from

console.log(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 code is using the handleClick function while the second code is calling the function

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 and sets the value of the class attribute of the specified element.

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

contains

23
Q

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

A

element.textcontent = ‘string’

24
Q

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

A

no

25
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,

26
Q

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

A
27
Q

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

A

focus

28
Q

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

A

blur

29
Q

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

A

input

30
Q

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

A

submit

31
Q

What does the event.preventDefault() method do?

A

prevents default action of the event

32
Q

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

A

browser would reload the page with form’s values in the URL

33
Q

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

A

.elements

34
Q

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

A

.value

35
Q

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

A

could be writing bugs

36
Q

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

A

when writing code, you can see errors as they happen

37
Q

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

A

no, created and stored in a variable

38
Q

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

A

.appendChild

39
Q

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

A

name, value (attribute name, value)

ex. .setAttribute(‘class’, ‘click-button’)

40
Q

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

A

create the element and store in a variable, give it content, call querySelector to find element and store in variable, append child and add it to the dom

41
Q

What is the textContent property of an element object for?

A

to return all the text content including all elements (set or get)

42
Q

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

A

.className property & .setAttribute() method

43
Q

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

A

only have to write function once so it can be reusable, and have a name to reuse the function

44
Q

What is the event.target?

A

where the event started

element u clicked on

45
Q

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

A

event bubbling

46
Q

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

A

.tagName

all caps

47
Q

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

A

string of a valid css selector

return closest ancestor element or itself

48
Q

How can you remove an element from the DOM?

A

.remove() method

49
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 event listener to an ancestor