Hackforge Academy

Category: javascript

dom creating adding and removing elements

Published on 30 Sep 2026

Explanation

createElement() creates a new HTML element in memory.

Code:

const li = document.createElement('li');
li.textContent = 'New Item';

Explanation

appendChild() adds a newly created element as the last child of a parent element.

Code:

const li = document.createElement('li');
li.textContent = 'New Item';

document.getElementById('list')
  .appendChild(li);

Explanation

append() can add one or more nodes or text values to an element.

Code:

const list = document.getElementById('list');

const li = document.createElement('li');
li.textContent = 'JavaScript';

list.append(li);

Explanation

remove() removes an element from the DOM.

Code:

const item = document.getElementById('item');

item.remove();

Explanation

A complete dynamic DOM operation can create an element, configure it, add it to the page, and later remove it.

Code:

const button = document.createElement('button');
button.textContent = 'Delete';

button.addEventListener('click', () => {
  button.parentElement.remove();
});

document.body.append(button);

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community