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

216
Views
how to disable all the click events and re-enable them based on a Boolean, which will be coming from a function

I am having some click events(more than 100) in my JS file, and for example code looks like this.

$(document).on('click', '.a', function(){
        alert($(this).text());
    });
    $(document).on('click', '.b', function(){
        alert($(this).text());
    });
    $(document).on('click', '.c', function(){
        alert($(this).text());
    });
    $(document).on('click', '.d', function(){
        alert($(this).text());
    });

This is just example code, my Click events doing a lot of things, and I can not change the click event code,

Now I have a Boolean which I am getting from a Function based on some condition. So On first click I want to see if this variable is True then only my click event on particular item should trigger, else click event should not trigger.

        
let tre = false;//it could be true or false based on function given below based on some condition.
        
function test(){
  return tre;
}
  $(document).on('click', '.a', function(){
            alert($(this).text());
        });
        $(document).on('click', '.b', function(){
            alert($(this).text());
        });
        $(document).on('click', '.c', function(){
            alert($(this).text());
        });
        $(document).on('click', '.d', function(){
            alert($(this).text());
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section>
    <div class="a">a</div>
    <br>
    <div class="b">b</div>
    <br>
    <div class="c">C</div>
    <br>
    <div class="d">d</div>
</section>

Now my Requirement is if I click on any item in the page (a,b,c,d) and if that varibale tre is TRUE then only my alert should work.

I can wrap all my click events in a function if needed, but can't add if condition inside every click event.

about 4 years ago · Juan Pablo Isaza
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!