DOM Creation and Event Delegation Flashcards

1
Q

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

A

No, but it creates it.

appendChild() method inserts a new element into the page.

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

appendChild()

There is also a .append method

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

Name and Value

h1.setAttribute(‘class’, ‘example’)

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

Create the element node

Query selector for the parent element

Append the new element to the parent element

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 pull or modify the text content within the tags of the element

Reading and Writing, Getting and Assigning

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

element. setAttribute(name, value);

element. className = ‘value’

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 create something (like the work of creating a DOM tree)?

A

Reusability and Accessibility

It makes it dynamic

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

What is the event.target?

A

This is the target element for the event

The target property of the event interface is a reference to the object onto which the event was dispatched

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 in its descendent elements?

A

Because when you interact with the parent element node, it interacts with all children element nodes

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

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 the closest parent element and returns the element type

It is a selector. It takes a string as it’s argument in the format of a css selector, and it returns itself or it’s nearest matching ancestor

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

You use to .remove method of the childNode object

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 would you avoid adding an event listener to every new element individually?

A

Event delegation

You can put your eventListener function on the parent element to spy on its children

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