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

187
Visualizações
how to add eventlistner to button added by a class which is method of class itself

I am making a module which shows a modal. I added a button on modal to close it and added this.hide as it's onclick but this does not closes the modal

code:

class Modal {
  constructor(content, allowclose, onclose) {
    this.content = content;
    this.allowclose = allowclose || true;
    this.onclose = onclose;
    // console.log(this.content)
    document.body.innerHTML += `<div id='modal-js-bg'><div id='modal-js-main'><div id='modal-js-content'>${this.content}</div>
<div id='modal-js-close'><button id='modal-js-close-btn' disabled=${!this.allowclose} onclick=${this.hide}>Close</button></div></div></div>`
  }

  show() {
    document.getElementById('modal-js-bg').style.display = 'flex';
    document.getElementById('modal-js-main').style.animation = 'showmodal 325ms ease-in';
  }

  hide() {
    document.getElementById('modal-js-bg').style.display = 'none';
    document.getElementById('modal-js-main').style.animation = 'hidemodal 325ms ease-out';
  }


}
#modal-js-bg{
  width: 100vw;
  height: 100vh;
  position: absolute;
  background-color: rgba(0,0,0,0.6);
  display: none;
  justify-content: center;
  align-items: center;
}
#modal-js-main{
  background-color:white;
  width:50%;
  padding:10px;
  display: flex;
  font-size:18px;
  align-items: center;
  flex-direction:column;
}
#modal-js-close-btn{
  background-color: #2e2eff;
  height:35px;
  width: 80px;
  border-radius: 7px;
  color: white;
  border: none;
}

@keyframes showmodal {
  0%{
    opacity:0;
  }
  100%{
    opacity:1;
  }
}
@keyframes hidemodal {
  0%{
    opacity:1;
  }
  100%{
    opacity:0;
  }
}
<html>
<head>
<title>modal</title>
</head>

<body>
  <script>
    window.onload = function () {
      const mymodal = new Modal('this is example')
      mymodal.show()
      }
  </script>

</body>

</html>

on document tree i can see that onclick function is written but every thing is written in small letters getElementById is written as getelementbyid I am not able to solve this can anyone help Thanks in advance

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