Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!