DOM Flashcards

1
Q

Why do we log things to the console?

A

To make sure our code works

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 an object that is typically smaller than the object that is being modeled

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 being loaded into a browser

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

Each individual elements on the html page (including the document itself)

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

What is a DOM Tree?

A

The drawn mapping of the elements of the html page

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(‘css selector’)

getElementById(‘id’)

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(‘css selector’)

getElementsByClassName(‘class’)

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

Why might you want to assign the return value of a DOM query to a 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(variableName)

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 of the elements in 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

It takes any valid CSS selector as its argument

Returns the first matching element

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 any valid CSS selector as its argument

Returns all matching elements in 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

To make sure our code works

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

Events are any changes to a document whether it be from user interaction or changes from within the browser.
Event handlers trigger functions depending on the event

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

They represent optional arguments

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

element.addEventListener(‘event’, callbackFunction);

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

What is a callback function?

A

A function that is passed as an argument in another function’s call

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

It’s full html element being targeted by a dom query

You could check by logging onto the console or check mdn documentation

20
Q

What is the difference between these two snippets of code?

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

A

The first one takes a callback function as the argument and the second one runs the function and whatever that is returned will be used as the argument for the addEventListener method

21
Q

What is the className property of element objects?

A

It accesses the class attribute of an html element

22
Q

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

A

Query the dom for an element then use the className property to assign a string value to update the class name

23
Q

What is the textContent property of element objects?

A

It accesses all text within the opening and closing tags of an element

24
Q

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

A

Query the dom for an element then use the textContent property to assign a string value to update the text content

25
Q

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

A

No, sometimes you’ll want to write code that applies to other parts of the html document not pertaining to the element the event it targeting

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. Probably would have to use a loop

27
Q

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

A

If you store information in the DOM, other parts of your javascript code could potentially change it

28
Q

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

A

No it only creates a reference to a dom element

29
Q

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

A

$parent.appendChild($newElement)

30
Q

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

A

2 arguments: first argument is the html attribute, second argument is the value

31
Q

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

A

createElement(), setAttribute()(optional), createTextNode()(optional), appendChild()

32
Q

What is the textContent property of an element object for?

A

Sets or gets the text within the opening and closing tags of an element

33
Q

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

A

setAttribute(‘class’, ‘value’)

$element.className = ‘value’

34
Q

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

A

You can reuse that function in several parts of your code

It’s easier to debug a function rather than a whole js document

35
Q

What is the event.target?

A

It references the object in which the event was directed at

36
Q

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

A

Using event flow, you can bubble up to the descendent’s ancestor

37
Q

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

A

tagName

38
Q

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

A

It takes a selectors as an argument and

It returns the closest element with that selector name to the element that called closest()

39
Q

How can you remove an element from the DOM?

A

element.remove()

40
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

Event delegation