Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How could I click any html element and it would just delete/remove itself with javascript?

been working on a thing for fun. I was wondering how I could delete an html element if I just clicked it on any website or html file. No jquery or other libraries, just vanilla JS. If the question is hard to understand here’s an example:
-Website loads
-I see a p tag or img tag or just any tag and I don’t want to see it any more so I just click it and it deletes.
-I’m now happy because of what I’ve done.

currentCode:

window.addEventListener("click", function(i) {
  //Code to remove something? idk... Not that good with js atm...
}, false);```

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

window.addEventListener("click", function(e) {
  const target = e.target;
  target.remove();
}, false);
<button>a</button>
<button>a1</button>
<p>paragraph...</p>
<h1>header...</h1>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading