DOM

Source: https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/dom-manipulation-and-events

Create Element

const div = document.createElement('div');

Append Element

append childNode as the last child of parentNode
parentNode.appendChild(childNode)

insert newNode into parentNode before referenceNode
parentNode.insertBefore(newNode, referenceNode)

Remove Element

Remove child from parentNode on the DOM and return a reference to child
parentNode.removeChild(child)    

Inline Style

const div = document.createElement('div');  
div.style.color = 'blue';                                      
// adds the indicated style rule

div.style.cssText = 'color: blue; background: white';          
// adds several style rules

div.setAttribute('style', 'color: blue; background: white');    
// adds several style rules

div.style.background-color // doesn't work - attempts to subtract color from div.style.background
div.style.backgroundColor // accesses the div's background-color style
div.style['background-color'] // also works
div.style.cssText = "background-color: white" // ok in a string

Editing Attributes

Classes

Text and HTML content

Attaching listeners to groups of nodes

Dom - Notes

Notes made from JavaScript DOM Crash Course - Traversy Media, source: https://youtu.be/0ik6X4DJKCc

Starter Template

Part 1

1. Examine the document object

2. Get Element by ID

3. Get Element by Class

4. Query Selector

5. Query Selector All

Part 2

1. Traversing the DOM

2. Create Element

Part 3

Events

Part 4

Real Example

Last updated

Was this helpful?