JavaScript - Remove Element on Click

Here is a JavaScript program example to remove an element on click.

JavaScript Remove Element on Click Example

In the following example, it will create five paragraphs, and when the user clicks on any paragraph, the JavaScript program will remove that element.

The Script for the HTML Head Part:

        padding: 20px;
        margin: 10px 0;
        width: 400px;
        background-color: #eeeeff;
    window.onload=function() {
        var paras = document.getElementsByTagName("p");
        for (var i = 0; i < paras.length; i++)
    function pruneparagraph() {
        var parent = this.parentNode;
        alert("paras " + document.getElementsByTagName("p").length);

The Script for the HTML Body Part:

    This is paragraph one
    This is paragraph two
    This is paragraph three
    This is paragraph four
    This is paragraph five


As shown in the featured image of this article.

Leave a Comment