DOM Flashcards

1
Q

Why do we log things to the console?

A

To check if we got the desired results.

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

What is a “model”?

A

An example 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

The HTML document / whatever DOCTYPE is being declared

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
  • JavaScript objects
  • Represents different parts of the page that is loaded
  • The document represented as a large object that holds all info about its content
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a DOM Tree?

A

The model of the HTML webpage as a tree of objects.

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: Selects an element by a specified ID

querySelector: CSS selectors

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
  • getElementsByClassName
  • getElementsByTagName
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

Saves the browser time looking for the element again. Also makes it easier to reuse.

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(): Selects an element by specifying the ID

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 access all 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

It takes CSS selectors and returns the first element that matches that selector/group of selectors.

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 CSS selectors and returns a NodeList with a list of the document’s elements that match the specified group of 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 can be selected for event handling to trigger code that responds to users.

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

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

What is a callback function?

A
  • A call back is when a function is passed into another function as an argument.
  • A function that you don’t call but is called when an event occurs.
  • ex) $hoverButton.addEventListener - when mouseover happens, the function handleMouseover fires
17
Q

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

A

The event object

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
  • Can find more info on MDN
  • A reference to the object where the event is occurring.//object that triggered the event
  • Can be checked by console.log
  • The element that the user interacts with
19
Q

What is the difference between these two snippets of code?

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

A

No ()= function only runs when event is fired.
- A callback function// The definition of the function
- When using an addEventListener, the function should always be a callback function

() = The function with no parameters. . Will run the function before the page finishes loading., instead of when the event occurs.

20
Q

What is the className property of element objects?

A

The class attribute of the object.

21
Q

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

A

Using the className property of the element object and assigning a value.

22
Q

What is the textContent property of element objects?

A

The text content of the node and its descendants.

23
Q

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

A

Using the textContent property of the object and assigning a value with an assignment operator.

24
Q

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

25
Would this assignment (DOM-manipulation) be simpler or more complicated if we didn't use a variable to keep track of the number of clicks?
More complicated
26
Why is storing information about a program in variables better than only storing it in the DOM?
It is easily retrievable and caching the elements allows for the browser to find it quicker
27
Does the document.createElement() method insert a new element into the page?
No
28
How do you add an element as a child to another element?
appendChild()
29
What do you pass as the arguments to the element.setAttribute() method?
A source attribute and the value.
30
What steps do you need to take in order to insert a new element into the page?
Create element, give it a class name (optional ) give it an attribute (optional) , give it text content ( optional) , query for an element already on page, append child.
31
What is the textContent property of an element object for?
To assign text content to an element.
32
Name two ways to set the class attribute of a DOM element.
className, set attribute to class and value of class name.
33
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
- You don’t need to manually do everything - Ensures everything is consistent.
34
What is the event.target?
- It retrieves the object from which an event was dispatched from - The element that was interacted with
35
Why is it possible to listen for events on one element that actually happen its descendent elements?
The event travels down to the point of interaction (event bubbling)
36
What DOM element property tells you what type of element it is?
event.target.tagName
37
What does the element.closest() method take as its argument and what does it return?
- Event.target at the beginning and takes the css selector - It will return the closest element it was nested in
38
How can you remove an element from the DOM?
element.remove();
39
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?
Add an event listener to the parent tag