DOM Flashcards

1
Q

Why do we log things to the console?

A

To check that the correct output is being made, debugging, knowing what you’re working with

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

What is a “model”?

A

Represents the structure of the document. Recreation, representation

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

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 data type object in javascript

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

What is the DOM?

A

Dom is a javascript object that has been constructed to recreate the content of the HTML document

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

What is a DOM Tree?

A

An element and all of its configurations and content represented as an object in the dom.

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

Give two examples of document methods that retrieve a single element from the DOM.

A

getElementById() and document.querySelector()

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 at once.

A

document.querySelectorAll()

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

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

A

If your script needs to use the same element more than once

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

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

A

console.dir()

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

Why would a <.script> 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
12
Q

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

A

string of Css selector and returns only the first of the matching elements

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

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

A

Css selector and returns Nodelist(collection of nodes)/ all of those elements that match

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

Why do we log things to the console?

A

To see the result of our output, to know what we are working with

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

Event - something that occurred. Listener is waiting and calls event handler. The purpose of events is to react to them through event handling, the next step to do

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

no

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

addEventListener(‘name of event as a string’, function you want to run)

18
Q

What is a callback function?

A

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.

19
Q

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

A

An object that has properties to describe the event that occurred

20
Q

What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?

A

Element that triggered the event, where did it begin.
Console log and mdn

21
Q

What is the difference between these two snippets of code?

A

element.addEventListener(‘click’, handleClick) - passes the function as a value, a call back, when event occurs
element.addEventListener(‘click’, handleClick()) - runs the function right now and replaces it with return value of undefined. Breaks and will not add event listener

22
Q

What is the className property of element objects?

A

Get or set the class attribute of element

23
Q

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

A

element.className = ‘ ’;

24
Q

What is the textContent property of element objects?

A

get or set the text content of the element

25
Q

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

A

element.textContent = ‘ ‘;

26
Q

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

A

no

27
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

complicated

28
Q

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

A

easy access for javascript instead of depending on another language to convert So we don’t have to use nodes in the dom and have easy access to the variables

29
Q

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

A

no

30
Q

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

A

parentelement.appendChild(childelement);

31
Q

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

A

element.setAttribute(‘name of value’, ‘value’)’; ex: ‘class’, ‘container’

32
Q

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

A

Create element, configuration like text content, applyc lasses, then query for parent element, append to parent.appendChild(elementhere);

33
Q

What is the textContent property of an element object for?

A

Adding text to a element or see what it is

34
Q

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

A

elementname.setAttribute(‘name of value’, ‘value’)’ or element.classname = ‘ ‘;

35
Q

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

A

Reusable for any situation using the same
no need to type everything out again

36
Q

What is the event.target?

A

Target of the event

37
Q

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

A

Event bubbling

38
Q

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

A

event.tagName

39
Q

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

A

A string of css selector and returns its closest ancestor element thats matching the css selector

40
Q

How can you remove an element from the DOM?

A

element.remove();

41
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

Use event delegation so add an even listener on the parent element