DOM Flashcards

1
Q

Why do we log things to the console?

A

So we can verify the outputs. Further, troubleshoot and
debugging.

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

What is a ”model”?

A

A model is a representation(or recreation) of something. In the case of DOM, it
is a copy of the page layout before any changes are applied

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 document is the entire 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

The objects are JavaScript objects

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

What is a DOM Tree? A DOM Tree is a model of the HTML code.

A

It arranges the elements in a
HTML file by their elements and relationship to each other

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

We can use getElementById() and querySelector() to select single elements from the DOM

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

So we can console log and verify we selected the correct element or to use it to modify the page later on

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() is used to inspect an element’s properties in the console

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

It is placed at the bottom because a browser needs to run all of the HTML first before we 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 in a string that must also be a valid CSS selector. It will return an element object that represents the first element in the document that matches the specified CSS 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

It takes in a string that must also be one or many valid CSS selectors separated by commas. It will return a list with the elements that corresponds to the CSS selectors

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

Events are used for detecting when a user interacts with a web page. Event handling is a way to respond to the user’s interactions

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

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(’Event’,functionName), ’Boolean’ (default: false. Optional)

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, which is then invoked inside the outer function to complete some kind of routine or action.

17
Q

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

A

The Event object. The
content is all of the data regarding the object that has occurred

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

The target property of the Event interface is a reference to the object onto
which the event was dispatched. It shows which element the effect is taking place for. If we are unsure, we can check MDN. It is the element where the event occurred, not the element the event was applied

19
Q

What is the difference between these two snippets of code?
– element.addEventListener(’click’, handleClick)
– element.addEventListener(’click’, handleClick())

A

First one is referencing a function where the second one uses the handleClick’s function’s return value as an argument

20
Q

What is the className property of element objects?

A

The className property of the Element interface
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

We can use className property of the DOM

22
Q

What is the textContent property of element objects?

A

The textContent property of the Node interface
represents the text content of the node and its descendants

23
Q

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

A

We use textContent property. Select
the element with a DOM query and then assign the textContent of that selection to a new text content

24
Q

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

A

No, because we do not always need it to be calledback

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

Harder. Because we would need to create a new DOM everytime we want to update
the page

26
Q

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

A

This way we can refer back to it whenever we want

27
Q

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

A

No. It creates the
elements

28
Q

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

A

we use the appendChild() method

29
Q

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

A

A string of the name of
the attribute and a value of the attribute

30
Q

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

A
  1. Create the element – createElement()
  2. Give it content – createTextNode()
  3. Query select an element to append the element to – document.querySelector()
  4. Append it to a child or before a selected DOM node – appendChild() or insertBefore(
31
Q

What is the textContent property of an element object for?

A

This is used for adding, changing, or
modifying the text content of the node and its decscendants

32
Q

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

A

We can use element.setAttribute() or
using class name property

33
Q

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

A

We can reuse the function elsewhere and use the return value elsewhere as well

34
Q

What is the event.target?

A

This will gives us the most specific element the event is interacting with

35
Q

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

A

The tagName property

36
Q

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

A

It takes in CSS
selectors. The function returns the closest ancestor element or itself which matches the selectors

37
Q

How can you remove an element from the DOM?

A

We can use the remove() method of the element
object

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

We can add an event listener to the
parent of the list and use a function to determine if the child is being clicked or not. Or use event
delegation