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

140
Visualizações
jQuery .click don't execute following code if element got clicked

This is most likely a duplicate, but I can't find something how to do it and asking for advice to push me in the right direction

With the following code I want to check which button got clicked and then do some stuff. It is working, but if for some weird reason the user is able to click the other button too, my variable would get overridden.

That is the right way of doing it right? To have a solution for every possible way that could happen and not to ignore it and say "it will never happen anyway"?

So the best thing would be (I think) to stop (break) the code (the second .click function) if the first one has already been clicked and vice versa.

The first thing that comes in mind is an if statement, but I'm not really sure how I would set this up and can check if one button has already has been clicked.

$(document).ready(function() {

  let yourOrganisation;

  $('.donate_btn').click(function() {
    $('.donate_button_wrapper').hide();
    $('.donate_finish_wrapper').show();
    $(window).scrollTop(0);
  });

  $(".donate_button_1").click(function() {
    yourOrganisation = "Organisation 1"
    console.log(yourOrganisation);
  });

  $(".donate_button_2").click(function() {
    yourOrganisation = "Organisation 2"
    console.log(yourOrganisation);
  });


});
.donate_finish_wrapper {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="donate_button_wrapper">
  <button class="donate_btn donate_button_1">
    Button 1
  </button>
  <button class="donate_btn donate_button_2">
    Button 2
  </button>
</div>

<div class="donate_finish_wrapper">
  Some content
</div>

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

0

What about disabling the other button?

$(document).ready(function() {

  let yourOrganisation;

  $('.donate_btn').click(function() {
    // commented out to show the disabled method
    // $('.donate_button_wrapper').hide();
    $('.donate_finish_wrapper').show();
    $(window).scrollTop(0);

    if($(this).hasClass('donate_button_1')) {
      $('.donate_button_2').attr('disabled','disabled');
      yourOrganisation = "Organisation 1"
    }

    if($(this).hasClass('donate_button_2')) {
      $('.donate_button_1').attr('disabled','disabled');
      yourOrganisation = "Organisation 2"
    }
  });
});
.donate_finish_wrapper {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="donate_button_wrapper">
  <button class="donate_btn donate_button_1">
    Button 1
  </button>
  <button class="donate_btn donate_button_2">
    Button 2
  </button>
</div>

<div class="donate_finish_wrapper">
  Some content
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You could check if the variable "yourOrganisation" has already been set:

if(!yourOrganisation) {yourOrganisation = "Organisation 1";}

and

if(!yourOrganisation) {yourOrganisation = "Organisation 2";}
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