- server.js
- browser.js
- Zooming User Interface & SVG Drawing Support
- dom-zoom custom element
- Node is a Dom Element like node with enhanced functionality
Here is a simple cheat sheet for methods related to creating and removing DOM elements in JavaScript. These methods are essential for manipulating the DOM dynamically:
-
document.createElement(tagName)
- Creates a new element with the specified tag name.
- Example:
let newDiv = document.createElement('div');
-
element.setAttribute(attribute, value)
- Sets an attribute on an element (like class, id, etc.).
- Example:
newDiv.setAttribute('class', 'my-class');
-
element.appendChild(child)
- Adds a child element to a parent element.
- Example:
document.body.appendChild(newDiv);
-
document.createTextNode(text)
- Creates a text node that can be appended to an element.
- Example:
let textNode = document.createTextNode('Hello, world!'); newDiv.appendChild(textNode);
-
element.innerHTML
- Sets or retrieves HTML content inside an element.
- Example:
newDiv.innerHTML = '<p>Some content</p>';
-
element.remove()
- Removes the element from the DOM.
- Example:
newDiv.remove();
-
parentNode.removeChild(child)
- Removes a child element from its parent node.
- Example:
document.body.removeChild(newDiv);
-
element.innerHTML = ''
- Clears the content of an element.
- Example:
newDiv.innerHTML = '';
// Create a new div with text
let newDiv = document.createElement('div');
let textNode = document.createTextNode('This is a new div');
newDiv.appendChild(textNode);
document.body.appendChild(newDiv);
// Remove the div after 3 seconds
setTimeout(() => {
newDiv.remove();
}, 3000);
- Create:
document.createElement()
,document.createTextNode()
,setAttribute()
- Add to DOM:
appendChild()
,innerHTML
- Remove from DOM:
remove()
,removeChild()
,innerHTML = ''
This cheat sheet covers the basics for creating and removing DOM elements, which should cover most needs for dynamic page manipulation.