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.