DOM Flashcards

1
Q

What is a “model”?

A

a tree

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

Which “document” is being referred to in the phrase Document Object Model?

A

html file

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

What is the word “object” referring to in the phrase Document Object Model?

A

data type objects

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

What is a DOM Tree?

A

model structure

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

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

A

.querySelector(), .getElementById()

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

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

A

.querySelectorAll()

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

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

A

for reusability and so you can access other information within the DOM object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
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
9
Q

Why would a

 tag need to be placed at the bottom of the HTML content instead of at the top?
A

so that when the user first load the page, all content can get loaded up first before DOM

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

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

A

string of css selector, return first matched item

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

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

A

string of css selector, return nodeList of all the matched items

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

What is the purpose of events and event handling?

A

prepare of incoming actions and response with something (interactive)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
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
14
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
15
Q

What is a callback function?

A

function that is passed into another function (not calling it)

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

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

A

event data

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

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

A

event target is the element that the event originated from, console log it, MDN

18
Q

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

A

1 - is set with a callback function
2 - the function will run on opening the webpage

19
Q

what is event bubbling?

A

event that happen to the element, it happens to the ancestor elements as well
Div > button -> click on button = click on Div

20
Q

What is the className property of element objects?

A

value of the attribute class of HTML document

21
Q

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

A

element.className = ‘new_value new_value_2’

22
Q

What is the textContent property of element objects?

A

property that hold the text for that element

23
Q

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

A

element.textContent = ‘new value’

24
Q

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

A

no

25
Q

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

A

doesn’t make javascript depends on other language when accessing data

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

parent.appendChild(child_node)

28
Q

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

A

(‘attribute_name’, ‘value’)

29
Q

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

A
  • create new element -> store it to variable
  • add necessary attribute/value/text
  • append this element to any other parents (if needed)
  • querySelector() parent element that is existed on the page
  • then append to parent element
30
Q

What is the textContent property of an element object for?

A

to insert text content to any element

31
Q

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

A

element.setAttribute(), document.className

32
Q

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

A

so it can be dynamic and create any amount of data depend on input data

33
Q

What is the event.target?

A

where the event occur

34
Q

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

A

event bubbling

35
Q

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

A

.tagName or nodeName (return as string all CAP)

36
Q

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

A

CSS selector
return element selected first found

37
Q

How can you remove an element from the DOM?

A

element.remove()

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

adding even listener to the parent and target specific tag name when event trigger

39
Q

What is the affect of setting an element to display: none?

A

not displaying item and completely remove it from the document flow

40
Q

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

A

CSS selector, return boolean (true or false)

41
Q

How can you retrieve the value of an element’s attribute?

A

.getAttribute(‘attribute_name’)