Code snippet: Remove button element on the click event of itself using JavaScript

If you need to use the button to remove it self, you can use the following code.

Let’s suppose you have the following element in your html document.

[HTML Code]

<input type="button" value = "Remove" class="subfilter" 
                             onclick="RemoveItself(this);" />

The following JavaScript code is using the parentNode.removeChild() method to remove the button. We are passing the element itself in to its click event.

[JavaScript Code ]

<script>
  function RemoveItself(elem) {
        elem.parentNode.removeChild(elem);
    }
</script>
  • Stan

    PERFECT!!!! at last, the first solution which is actually nice and easy, saved me a lot of time