JavaScriptEasy
How do you add, remove, and modify HTML elements using JavaScript?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
To add, remove, and modify HTML elements using JavaScript, you can use methods like createElement, appendChild, removeChild, and properties like innerHTML and textContent. For example, to add an element, you can create it using document.createElement and then append it to a parent element using appendChild. To remove an element, you can use removeChild on its parent. To modify an element, you can change its innerHTML or textContent.
js
// Adding an element
const newElement = document.createElement("div");
newElement.textContent = "Hello, World!";
document.body.appendChild(newElement);
// Removing an element
const elementToRemove = document.getElementById("elementId");
elementToRemove.parentNode.removeChild(elementToRemove);
// Modifying an element
const elementToModify = document.getElementById("elementId");
elementToModify.innerHTML = "New Content";
