DOM Flashcards

1
Q

Why do we log things to the console?

A

to see what our code is doing

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 else

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

referring to the JavaScript objects that the DOM reads the document as

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

What is a DOM Tree?

A

all the nodes of an element; a model of a web page the browser stores in memory (DOM element plus all the children inside 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

getElementById( )

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

getElementByClassName( )
getElementByTagName( )
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

if you want to work with that element node more than once

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

the browser needs to parse all the elements of the HTML page before the JavaScript code can access them

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

argument: a string containing a CSS selector describing the element you want to work with
return: ONE HTMLElement object representing the first element in the document that matches the selector

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

argument: DOMString containing one or more selectors
return: a NodeList containing one Element object for each element that matches at least one of the specified selectors, otherwise an empty NodeList if none match

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

to see how our code is working

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

allows your page to be interactive

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

What do [ ] square brackets mean in function and method syntax documentation?

A

it means it is optional to include

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( )

17
Q

What is a callback function?

A

a function that gets passed into another function as an argument which is called within the outer function to do some action

  • a function being passed as data
18
Q

What object is passed into an event listener callback when the event fires?

A

the event object

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

An HTML element that triggered this event

you can console log it and check mdn for more information

20
Q

What is the difference between these two snippets of code?

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

A

the first one runs the handleClick function when the event is fired

the second one indicates that the handleClick function should run as the page loads

21
Q

What is the className property of element objects?

A

string value containing each class for the element separated by spaces

22
Q

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

A

elm.setAttribute(‘class’, elm.getAttribute(‘class’))

23
Q

What is the textContent property of element objects?

A

represents the text content of the node and its descendants.

24
Q

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

A

use textContent property on the element object and assign it a new value

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 difficult

27
Q

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

A

allows to reuse more than once simply and efficiently, easier to read

28
Q

What does the transform property do?

A

lets you rotate, scale, skew or translate an element

29
Q

Give four examples of CSS transform functions.

A

matrix, translate, rotate, skew, scale

30
Q

What is the event.target?

A

target property of event interface, reference to element where the event originated from

31
Q

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

A

bubbling (elaborate)

32
Q

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

A

tagName?

33
Q

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

A

argument: selector
return: closest ancestor

34
Q

How can you remove an element from the DOM?

A

remove( )

35
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

dom event delegation

putting an event listener on the element above the element you want it to originate on