Manipulating the DOM Flashcards
What is the process to add an element as a child of another on a webpage using js?
1 Assign an element to a variable
2 Create a new element in memory
3 Add attributes and styles to it
4 Insert it into the DOM. Append it to the parent
1 const container = document.querySelector('#container');
2 const content = document.createElement('div');
3
content. classList.add(‘content’);
content. textContent = ‘This is the glorious text-content!’;
4
container.appendChild(content);
<body> <h1> THE TITLE OF YOUR WEBPAGE </h1> <div id="container"> <div class="content"> This is the glorious text-content! </div> </div> </body>
How do you create a div in memory?
const div = document.createElement(‘div’);
How do you add a child node to a parent:
- at the end?
- before a specific node?
- after a specific node?
parentNode.appendChild(childNode) appends childNode as the last child of parentNode
parentNode.insertBefore(newNode, referenceNode) inserts newNode into parentNode before referenceNode
parentNode.insertBefore(newNode, referenceNode.nextSibling);
How do you remove a child node from a given parent?
parentNode.removeChild(child) removes child from parentNode on the DOM
returns a reference to child
What does parentNode.appendChild(childNode) do and/or return
Appends childNode as the last child of parentNode
Returns A Node that is the appended child (aChild), except when aChild is a DocumentFragment, in which case the empty DocumentFragment is returned.
What does parentNode.insertBefore(newNode, referenceNode) do and/or return?
Inserts newNode into parentNode before referenceNode
Returns Returns the added child (unless newNode is a DocumentFragment, in which case the empty DocumentFragment is returned).
What does parentNode.removeChild(child) do and/or return
parentNode.removeChild(child) removes child from parentNode on the DOM a
returns a reference to child
Add an inline style using js
- 1 style
- multiple styles
- by setting an attribute
const div = document.createElement(‘div’);
div.style.color = 'blue'; // adds the indicated style rule ADDS and OVERWRITES
div.style.cssText = ‘color: blue; background: white;’;
// adds several style rules
div.style works too
This also SETS the attribute to this value. It DOES NOT ADD. It REPLACES
div.setAttribute(‘style’, ‘color: blue; background: white;’);
// adds several style rules
This also SETS the attribute to this value. It DOES NOT ADD. It REPLACES
What does
div.style.background-color
do or return
an error
the - attempts to subtract the two values
if the css rule as a -, then you have to use camel case: backgroundColor or bracket notation: ['background-color']
How do you refer to a css rule that uses a -
like background-color? (can use dashes in js)
It's automatically converted to camel case: backgroundColor or you can use bracket notation: ['background-color']
Change an id name (or change any attribute) (2 ways)
div.setAttribute('id', 'theDiv'); // if id exists, update it to 'theDiv', else create an id // with value "theDiv"
or
div.id = “theDiv”
What does div.setAttribute(‘id’, ‘theDiv’); do and/or return
// if id exists, update it to 'theDiv', else create an id // with value "theDiv"
returns undefined
add,
remove,
and toggle
a css class
div.classList.add('classname'); // adds class "new" to your new div
div.classList.remove('classname'); // removes "new" class from div
div.classList.toggle('classname'); // if div doesn't have class "active" then add it, or if // it does, then remove it
What do these do and/or return
div. classList.add();
div. classList.remove();
div. classList.toggle();
div.classList.add('classname'); // adds class "classname" to your new div no return value
div.classList.remove('classname'); // removes "classname" class from div // no return value
div.classList.toggle('classname', boolean); // if div doesn't have class "classname" then add it, or if // it does, then remove it Returns A boolean value, true or false, indicating whether token is in the list after the call or not. Can do it based on any Boolean return.
Should you remove a class list or toggle it?
Toggle it
It has more functionality?