Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do you delete an element in HTML with Javascript?

How do you delete HTML elements via script? Thanks in advance...

Update:

is there a way to do it using getelementbyid? like document.get.....id("childDiv").removeSomething?

2 Answers

Relevance
  • ?
    Lv 6
    9 years ago
    Favorite Answer

    Yes, you use removeChild. Here's a function that let's you delete any element you want:

    function remove(elem) {

    if (typeof elem == "string") elem = document.getElementById(elem);

    elem.parentNode.removeChild(elem);

    }

  • MT
    Lv 4
    9 years ago

    You use the removeChild method.

    Assuming you have the following HTML div elements:

    <div id="ParentDiv">

        <div id="ChildDiv"></div>

    </div>

    A simple sample would be:

    var Parent = document.getElementById("ParentDiv");

    var Child = document.getElementById("ChildDiv");

    Parent.removeChild(Child);

    This will remove the child div.

    Hope this helps.

Still have questions? Get your answers by asking now.