DOM Flashcards

1
Q

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

A

No. It simply creates an empty element that can be added to the DOM tree

Insert a new element with:

  • appendChild()
  • insertBefore()
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()

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

(attribute name, value)

^ both in 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
1. Create a new element and store it in a variable. 
var newEl = document.createElement( 'li ');
2. Create a text node and store it in a variable. 
var newText = document.createTextNode('quinoa');
  1. Attach the new text node to the new element.
    newEl.appendChild(newText);
4. Find the position where the new element should be added. 
var position = document.getElementsByTagName('ul ')[0];
  1. Insert the new element into its position.
    position. appendChild(newEl);
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
  • represents text content of node and its descendants
  • if node is document or doctype, textContent returns null
  • returns concatenation of textContent of every child node
  • different from innerText, which only shows “human-readable” elements. textContent shows every element, even elements
  • set or get text content
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’)
ex: element.setAttribute(“style”, “background-color:red;”);

element.className = “className”;

third method:
add class list
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

so you don’t have to work as hard. automate the process.

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