[javascript] How to remove an HTML element using Javascript?

I am a total newbie. Can somebody tell me how to remove an HTML element using the original Javascript not jQuery.

index.html

<html>
<head>
 <script type="text/javascript" src="myscripts.js" > </script>
 <style>
 #dummy {
  min-width: 200px;
  min-height: 200px;
  max-width: 200px;
  max-height: 200px;
  background-color: #fff000;
 }
 </style>
</head>
<body>
 <div id="dummy"></div>

 <form>
  <input type="submit" value="Remove DUMMY" onclick="removeDummy(); "/>
 </form>
</body>

myscripts.js

function removeDummy() {
 var elem = document.getElementById('dummy');
 elem.parentNode.removeChild(elem);
}

What happens when I click the submit button, is that it will disappear for a very very short time and then appear back immediately. I want to completely remove the element when I click the button.

This question is related to javascript html

The answer is


You should use input type="button" instead of input type="submit".

<form>
  <input type="button" value="Remove DUMMY" onclick="removeDummy(); "/>
 </form>

Checkout Mozilla Developer Center for basic html and javascript resources


Change the input type to "button". As T.J. and Pav said, the form is getting submitted. Your Javascript looks correct, and I commend you for trying it out the non-JQuery way :)


Try running this code in your script.

document.getElementById("dummy").remove();

And it will hopefully remove the element/button.


It reappears because your submit button reloads the page. The simplest way to prevent this behavior is to add a return false to the onclick like so:

<input type="submit" value="Remove DUMMY" onclick="removeDummy(); return false;" />

This works. Just remove the button from the "dummy" div if you want to keep the button.

_x000D_
_x000D_
function removeDummy() {_x000D_
  var elem = document.getElementById('dummy');_x000D_
  elem.parentNode.removeChild(elem);_x000D_
  return false;_x000D_
}
_x000D_
#dummy {_x000D_
  min-width: 200px;_x000D_
  min-height: 200px;_x000D_
  max-width: 200px;_x000D_
  max-height: 200px;_x000D_
  background-color: #fff000;_x000D_
}
_x000D_
<div id="dummy">_x000D_
  <button onclick="removeDummy()">Remove</button>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Just do this element.remove();

Try it here LOOK

http://jsfiddle.net/4WGRP/


That is the right code. What is probably happening is your form is submitting, and you see the new page (where the element will exist again).


Your JavaScript is correct. Your button has type="submit" which is causing the page to refresh.


I'm still a newbie too, but here is one simple and easy way: You can use outerHTML, which is the whole tag, not just a portion:

EX: <tag id='me'>blahblahblah</tag>'s innerHTML would be blahblahblah, and outerHTML would be the whole thing, <tag id='me'>blahblahblah</tag>.


So, for the example, if you want to delete the tag, it's basically deleting its data, so if you change the outerHTML to an empty string, it's like deleting it.

<body>
    <p id="myTag">This is going to get removed...</p>
    <input type="button" onclick="javascript:
        document.getElementById('myTag').outerHTML = '';//this makes the outerHTML (the whole tag, not what is inside it)
    " value="Remove Praragraph">
</body>

Instead, if you want to just not display it, you can style it in JS using the visibility, opacity, and display properties.

document.getElementById('foo').style.visibility = hidden;
//or
document.getElementById('foo').style.opacity = 0;
//or
document.getElementById('foo').style.display = none;



Note that opacity makes the element still display, just you can't see it as much. Also, you can select text, copy, paste, and do everything you could normally do, even though it's invisible.
Visibility fits your situation more, but it will leave a blank transparent space as big as the element it was applied to.
I would recommend you do display, depending on how you make your webpage. Display basically deleting the element from your view, but you can still see it in DevTools. Hope this helps!


index.html

<input id="suby" type="submit" value="Remove DUMMY"/>

myscripts.js

document.addEventListener("DOMContentLoaded", {
//Do this AFTER elements are loaded

    document.getElementById("suby").addEventListener("click", e => {
        document.getElementById("dummy").remove()
    })

})