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

212
Visualizações
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
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