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

153
Visualizações
JavaScript - Removing Event Listener Not Working

On clicking button, an alert message pops up. The issues is that the alert message is popping up twice. I know its because of adding "click" event but I remove the event before adding the "click" event again. So why is it not working? Please run the snippet that I attached to this post to understand. Thank you

HTML

<html>
<div class="main"></div>
</html>

JAVASCRIPT

class RenderOne {

  renderOne() {

    return `

    <button class="add-btn">Add</button>

  `
  }
  renderData() {
    const mainContain = document.getElementsByClassName("main")
    mainContain[0].innerHTML = this.renderOne()
  }
  
  clickBtn() {
    const addBtn = document.getElementsByClassName("add-btn")
    addBtn[0].addEventListener("click", () => {
      alert("I was clicked")
    })
  }

  removeEvent() {
    const addBtn = document.getElementsByClassName("add-btn")
    addBtn[0].removeEventListener("click", this.clickBtn())
  }
}

class TestingMain {
  render() {
    const render = new RenderOne()
    render.renderData()
    render.removeEvent()
  }
}

class RunTest {
  renderRunTest() {
    const render = new RenderOne()
    render.removeEvent()
  }
}


let testingMain = new TestingMain()
let runTest = new RunTest()
testingMain.render()
runTest.renderRunTest()
about 4 years ago · Juan Pablo Isaza
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