Ads by ProfitSence
Close

Remove html element in JavaScript Code Example

Last Updated on Wednesday 5th Oct 2022

Example 1: Remove an Element by id

			
					function removeElement(elementId) {
    let element = document.getElementById(elementId);
    element.parentNode.removeChild(element);
}

			
	

Example 2: Delete element by class name

			
					function removeElementsByClass(className){
    const elements = document.getElementsByClassName(className);
    while(elements.length > 0){
        elements[0].parentNode.removeChild(elements[0]);
    }
}

			
	

Example 3:

			
					let element = document.querySelector('#some-element');
element.parentNode.removeChild(element);

//remove attribute
elem.removeAttribute('data-foo');