Skip to main content
TOPIC 2 OF 7 · 2 MIN READ

CRUD Operations on the DOM

CRUD (Create, Read, Update, Delete) operations are essential for interacting with the DOM.

Key concepts

Create

Add elements dynamically to the DOM.

Read

Access existing elements from the DOM.

Update

Modify content or attributes of DOM elements.

Delete

Remove elements from the DOM.

Examples

Create:

const newElement = document.createElement('div');
newElement.textContent = 'This is a new div';
document.body.appendChild(newElement);

Read:

const header = document.getElementById('title');
console.log(header.textContent); // Logs: "Welcome to the DOM!"

Update:

header.textContent = 'Updated Title';
header.style.color = 'blue';

Delete:

header.remove(); // Removes the <h1> element

Full CRUD Operation:

// Create
const newPara = document.createElement('p');
newPara.textContent = 'This is a new paragraph.';
document.body.appendChild(newPara);

// Read
const para = document.querySelector('p');
console.log(para.textContent);

// Update
para.textContent = 'Updated paragraph content.';
para.style.fontWeight = 'bold';

// Delete
para.remove();

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus