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

168
Visualizações
How to output alert after conditions are met using javascript?

I want to do an alert() in the next click but it keeps alerting me whether I'm doing my first click.

function acquire(clicked_id){ 

    count++;
    var val = document.getElementById("qty").value; 
    let data = document.getElementById(clicked_id); 
    var disp = document.getElementById("display"); 
    
    
    if(data.classList.contains("taken")){
        alert("Seat is taken!");
        count--; 
    }else{
        document.getElementById(clicked_id).classList.add("taken") 
    }

    document.getElementById("display").innerHTML = count;

    if(count==val){
        alert("Exceeded!");
    }
}

image here

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

0

I suggest you delegate

I am not sure how you would want to "un-take" a seat

window.addEventListener("DOMContentLoaded", () => {
  const disp = document.getElementById("display");
  const val = document.getElementById("qty").value;
  const seats = document.getElementById("seats");
  seats.addEventListener("click", function(e) {
    const tgt = e.target.closest(".seat")
    if (!tgt) return; // not a seat or a child of seat

    if (tgt.classList.contains("taken")) {
      alert("Seat is taken!");
      return; // stop
    }
    tgt.classList.toggle("taken"); 
    const count = seats.querySelectorAll(".seat.taken");
    if (count.length > val) {
      tgt.classList.remove("taken");
      alert("Exceeded!");
    }
    else  document.getElementById("display").innerHTML = count.length;

  });
});
.taken {
  background-color: red;
}
<input type="hidden" id="qty" value="2" />
<div id="seats">
  <div id="seat1" class="seat">Seat 1</div>
  <div id="seat2" class="seat">Seat 2</div>
  <div id="seat3" class="seat">Seat 3</div>
</div>
<span id="display"></span>

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