Javascript - Remove element by tag name

By xngo on June 27, 2019

The Javascript function below will remove element by tag name(including all its sub-elements). This function is useful if you need to clear a section of your webpage and then add new data.

function removeElementByTagName(tagName)
{
    var element = document.getElementsByTagName(tagName);
    for (index = element.length - 1; index >= 0; index--)
    {
        element[index].parentNode.removeChild(element[index]);
    }
}

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.