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

160
Visualizações
When clicking span element, modal doesn't close

I'm creating two modals. One for buying, and the other for selling. However, when I click on the span in the modal which is supposed to close it, the modal isn't closed. Here's the code:

var buyModal = document.getElementById("buyModal");
var sellModal = document.getElementById("sellModal");

function buy() {
  buyModal.style.display = "block";
}

function sell() {
  sellModal.style.display = "block";
}

function close() {
  buyModal.style.display = "none";
  sellModal.style.display = "none";
}
<button id="buyButton" onclick="buy()">Buy a Currency Pair</button>
<button id="sellButton" onclick="sell()">Sell a Currency Pair</button>
<div id="buyModal" class="modal">
  <div class="modal-content">
    <button class="close" onclick="close()">&times;</button>
    <p>Buy</p>
  </div>
</div>
<div id="sellModal" class="modal">
  <div class="modal-content">
    <button class="close" onclick="close()">&times;</button>
    <p>Sell</p>
  </div>
</div>
</div>

Does anybody know why it doesn't work?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Close is a reserved JavaScript word and the browser refuses to call it in an event.

You must choose a new name (for example closeDiv):

var buyModal = document.getElementById("buyModal");
var sellModal = document.getElementById("sellModal");

function buy() {
  buyModal.style.display = "block";
}

function sell() {
  sellModal.style.display = "block";
}

function closeDiv() {
  buyModal.style.display = "none";
  sellModal.style.display = "none";
}
<button id="buyButton" onclick="buy()">Buy a Currency Pair</button>
<button id="sellButton" onclick="sell()">Sell a Currency Pair</button>
<div id="buyModal" class="modal">
  <div class="modal-content">
    <button class="close" onclick="closeDiv()">&times;</button>
    <p>Buy</p>
  </div>
</div>
<div id="sellModal" class="modal">
  <div class="modal-content">
    <button class="close" onclick="closeDiv()">&times;</button>
    <p>Sell</p>
  </div>
</div>

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