JavaScript Dom Flashcards

1
Q

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

A

No it does not.

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

parentElement.appendChild( childElement )

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

strings,

the first is the attribute name and the value of the attribute

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

var newEl = document.createElement() - create the element

var parentEl = document.quereySelector() - query the dom for the parent element to attach to

parentEl.appendChild(newEl) - add new el to parent el as a child of it

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

get and sets the text content of the element’s DOM node

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 method

.setAttribute( ‘class’, ‘className’)

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

We can reuse the function where we want a similar DOM tree structure.

Don’t have to rewrite code to create an element, can just use the function.

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

What is the event.target?

A

the element that was most immediately interacted with

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

Events occur starting from the most specific node bubbling outwards to least specific node.

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

tagName - returns element as a string in full CAPS

event.target.tagName

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

How can you remove an element from the DOM?

A

remove() method

element.remove()

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

Add it to the parent element containing each child DOM element, and do work when the event comes from specified element

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

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

A

Takes a CSS selector as as string as an arg and returns the first ancestor element that matches that selector

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