DOM Flashcards

1
Q

Why do we log things to the console?

A

Debugging and anytime we want to know the value a variable holds

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

What is a “model”?

A

Any 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

XML or HTML document that is currently loaded (and all of its info)

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 web page loaded in the current window or tab

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

What is the DOM tree?

A

A tree of all of the elements with the document node at its root.

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

Document.querySelector() & Document.getElementById()

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

Between Document.querySelector() & Document.getElementById() — which is faster? & why

A

getElementById b/c it doesn’t have to search

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

Give one example of a document method that retrieves multiple elements from the DOM.

A

Document.querySelectorAll (and Document.getElementsbyClass())

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

What is the difference between querySelectorAll() and getElementsByClass() ?

A

QuerySelectorAll = nodelist ; getElementsbyClass = HTML collection

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

Why might you want to assign the return value of a DOM query to a variable?

A

So you can call and manipulate it later

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

What console method allows you to inspect the properties of a DOM element object?

A

The .dir method

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

Why would a tag need to be placed at the bottom of the HTML document?

A

You can’t query HTML that isn’t loaded yet

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

What does document.querySelector() take as its argument and what does it return?

A

It takes a string with a selector and returns the element that matches that string —> else null

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

What does querySelectorAll() take as its argument and what does it return?

A

Takes a selector as a string as argument, returns a nodelist of all elements that match that selector.

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

What is the purpose of events and event handling?

A

Create and manage interactivity with the webpage!

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

Are all possible parameters required to use a JavaScript method or function?

A

Noooo — only the required ones (sometimes none)

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

What method of element objects lets you set up a function to be called when a specific type of event occurs?

A

Document.addEventListener()

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

What two traits define a callback function?

A
  1. A function passed into another function
  2. We don’t call it — it’s called by something else
19
Q

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

A

The event object

20
Q

What is the event.target? If you weren’t sure, how would you check?

A

It is the element that dispatches the event — we can console.log it — check MDN!

21
Q

Events have three phases… What are they?

A
  1. Capture phase (travel and locate)
  2. Fire on target (callback (passes in event object)
  3. Bubbling (bubbles back up to the top of the document)
22
Q

What is the difference between these two snippets of code?
FunctionOne, FunctionOne()

A

One runs the function, one is referring to the function definition

23
Q

What is the className property of element objects?

A

Gets and sets the value of the class attribute

24
Q

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

A

Object.className = ‘class-to-set’;
Object.classList also has various useful methods

25
Q

What is the textContent property of element objects?

A

Gets and sets the text content of the node and all its descendants

26
Q

How do you update the text within an element?

A

Object.textContent = ‘text-to-set’;

27
Q

Is the event parameter of an event listener always useful?

A

It is not always used!

28
Q

Why is storing information in variables better than storing info in the DOM?

A

It’s easier to manipulate data into the DOM instead of the other way around — lots of things can affect the DOM

29
Q

What method is better for adding specific classes or toggling them on and off?

A

classList.method() —> add, remove, replace, and toggle methods

30
Q

Why is innerHTML baaaad?

A

You can see all of HTML —> if somebody is malicious and finds that in your document they can run scripts to pull information from your website. Eval is the same way!

31
Q

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

A

No — it only creates that element.

32
Q

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

A

Use either the .append( ) or .appendChild( ) method

33
Q

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

A

Two arguments, (attribute, value) —> i.e. (src, “image-path”)

34
Q

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

A

Create the element, add any desired properties, values, class, etc., use .appendChild( )

35
Q

What is the textContent property of an element object for?

A

Getter/setter for text content of an item —> it will create the node!

36
Q

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

A

className = ‘ ’ ; classList.add/remove/toggle( ) ; setAttribute(‘class’, ‘class-to-add”)

37
Q

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

A
  1. By adding input arguments, you can cut down on time if you have similar html structure
  2. It allows easy manipulation of those elements because you already have them declared.
  3. When you update the values, the function will also update the page.
38
Q

What is the event.target?

A

The element that dispatched the event (it was interacted with)

39
Q

Why is it possible to listen for events on one element that actually happens to its descendants?

A

Bubbling!

40
Q

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

A

Event.target.tagName (or event.target.nodeName)

41
Q

All elements are _____ but not all nodes are _____.

A

Nodes. Elements.

42
Q

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

A

It takes a selector as its argument and returns the closest element (or itself) that matches the selector —> else returns null

43
Q

How can you remove an element from the DOM?

A

Use the element.remove( ) method!

44
Q

If you wanted to insert new clickable DOM elements into the page using JS, how could you avoid adding an event listener to every new element individually?

A

Add the event listener to the shared ancestor of those elements and when the event bubbles, use the event.target.tagName to determine which element fired the event