Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
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 anymore 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);
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

What about setting the element's display to none?

const foo = document.getElementById('foo');
const bar = document.getElementById('bar');

[foo, bar].forEach((el) => {
  el.addEventListener('click', () => el.style.display = 'none');
});
<div id="foo" style="border: 1px solid black; height: 50px; margin-bottom: 20px;"></div>
<div id="bar" style="border: 1px solid red; height: 50px;"></div>

EDIT: This time without IDs (this is a good intro to event bubbling):

// Look ma, no ids!
window.addEventListener('click', (event) => {
  event.target.style.display = 'none';
});
<div id="foo" style="border: 1px solid black; height: 50px; margin-bottom: 20px;"></div>
<div id="bar" style="border: 1px solid red; height: 50px;"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Here are two ways to achieve the same effect:-

document.getElementById("myDIV").style.display = "none"; // hides element

document.getElementById("myDIV").remove(); // removes element from the DOM
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda