DOM Flashcards
Does the document.createElement() method insert a new element into the page?
No. It simply creates an empty element that can be added to the DOM tree
Insert a new element with:
- appendChild()
- insertBefore()
How do you add an element as a child to another element?
appendChild()
What do you pass as the arguments to the element.setAttribute() method?
(attribute name, value)
^ both in strings!
What steps do you need to take in order to insert a new element into the page?
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');
- 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];
- Insert the new element into its position.
position. appendChild(newEl);
What is the textContent property of an element object for?
- 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
Name two ways to set the class attribute of a DOM element.
element. setAttribute(‘name’, ‘value’)
ex: element.setAttribute(“style”, “background-color:red;”);
element.className = “className”;
third method: add class list
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
so you don’t have to work as hard. automate the process.