I have a global flag variable. If this flag's value is true
then I add a lot of <div>
s to the DOM, otherwise nothing happens. After this I need to handle the click event on all of the previously added <div>
s. I can't add the click handler immediately after I created the <div>
s, because they are in different parts of the code, however the flag variable is still accessible. I add the click handlers this way:
$('.js-click').click(function() {
//Handle the click.
});
It doesn't matters if the <div>
s were added previously or not, the code above will work in any case, however because I have the global flag variable, I could add a condition before the jQuery code, so it would execute only if the <div>
s were created.
if(flag) {
$('.js-click').click(function() {
//Handle the click.
});
}
Does this makes any difference? Is there an amount of element or a type of jQuery selection where this condition would increase efficiency?