How to delete DIV element in JavaScript

Use parentElement.removeChild() and specify the ID of the element you want to remove.

<script>
    var element = document.getElementById('yourElementId')
    element.parentElement.removeChild(element);
</script>

Example:

<div id="myElementId">This element will be removed in JavaScript code.</div>

<script>
    var e = document.getElementById('myElementId')
    e.parentElement.removeChild(e);
</script>

Function for deleting DIV elements in JavaScript:

If you want to use this often, here’s the function that can make your job a little bit easier:

<script>
    function removeDiv(divId) {
        var e = document.getElementById(divId);
        e.parentElement.removeChild(e);
    }
</script>

Example:

<div id="myElementId">This element will be removed in JavaScript code.</div>
<div id="anotherElement">This is an element.</div>

<script>
    function removeDiv(divId) {
        var e = document.getElementById(divId);
        e.parentElement.removeChild(e);
    }

    // Example:

    removeDiv('myElementId');
</script>

Output:

This is an element.