Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda