JS dom creation and event delegation Flashcards

1
Q

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

A

no it inserts in the DOM tree and needs to be appended to the page later

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

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

A

parent.appendChild(child)

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

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

A

the attribute and the value

ex) attributes always stored as strings

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

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

A

createElement method, store new element in variable, query for parent element and call appendChild with new element as argument.

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

What is the textContent property of an element object for?

A

to change/set the text content (get or set)

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

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

A

className or setAttribute

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

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

A

you can reuse when more stuff is added later, easier to test….can describe code effectively

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

What is the event.target?

A

reference to the element where the event occurs-where delegation occurs

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

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

A

event bubbling

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

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

A

event.target.tagName

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

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

A

it takes a string with a selector and returns the closest ancestor element that matches that string

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

How can you remove an element from the DOM?

A

element.remove method

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

event.target.tagName needs to have a condition

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