DOM (Document Object Model) Flashcards

1
Q

Why do we log things to the console?

A

Inspect value to debug - This is especially important for DOM

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

What is a “model”? (Not in relation to code)

A

A prototype or 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

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

JavaScript object for a HTML element including attributes, values, and its children elements

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

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

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

Easier access so you can search for it and use it again in the future

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 (which stands for directory)

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

HTML content needs to load the data before using DOM or else it will have nothing to work with

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

String that consists of a CSS selector and 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

String that consists of a CSS selector and returns all those that match (node list)

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

Creating a reaction in response to something happening.

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 some are optional

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

Function within a function that is being passed as another argument

17
Q

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

A

Object that contains the data of that event

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

Where the event occurred.

You can check in the console.log

19
Q

What is the difference between these two snippets of code?

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

A

The first will run when the event fires.
The second will return the value which would be undefined. We will never call the event listener, the browser technically does this which is why the second code snippet is wrong.

20
Q

What is the className property of element objects?

A

Property stored on the DOM element object which stores the class attribute of that element

21
Q

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

A

className property with a value assignment

22
Q

What is the textContent property of element objects?

A

Allows us to retrieve or manipulate text content

23
Q

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

A

textContent property with a value assignment

24
Q

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

A

Not always - all the information isn’t always necessary

25
Q

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

A

You can reuse it later and you don’t want to intermingle your languages in a way where they become dependent on each other.

26
Q

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

A

No

27
Q

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

A

appendChild method

28
Q

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

A

String name and then the value

29
Q

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

A
  • createElement
  • Store the new element
  • Query for parent element
  • Call appendChild on parent with new element as an argument
30
Q

What is the textContent property of an element object for?

A

Stores textContent of the HTML element to retrieve or change

31
Q

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

A

className

setAttribute

32
Q

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

A

Reusable and its easier to debug and test for it’s intended purpose.

33
Q

What is the event.target?

A

Element where the event occurs

34
Q

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

A

Bubbling

35
Q

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

A

tagName

36
Q

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

A

String of CSS selector and returns closest ancestor element that matches pattern

37
Q

How can you remove an element from the DOM?

A

remove method

38
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 an eventListener to parent element and then run a condition to see if it is targeting an element