I have around sixty filter buttons in index.html
, each of which has a $().click()
event handler attached.
$(document).ready(function() {
$('.filter_button_class').click(function(e) {
console.log("filter button clicked");
e.preventDefault();
});
});
In my CSS file, the :active
selector ensures a filter button turns blue when it is clicked.
Now the code works fine, most of the time. When index.html
is first loaded in the browser, the event handlers all seem to attach correctly and the code always works as intended. It's a single page application, so when the user hits the back button to return to index.html
and clicks again on one of the filter buttons, it indeed turns blue but the code inside of the .click()
handler sometimes doesn't execute.
I suspect it might be because when index.html
is reloaded, not all the click handlers get attached before the user clicks a filter button. But when the user clicks the same filter button again (total of twice), the handler usually executes.
Question Is there a way to have the code inside of click()
wait to execute until all event handlers are attached first?
Instead of .click()
I have also tried .on()
but this has no effect. It might also be that the issue I'm identifying (event handlers attaching to elements too slowly) is wrong, but I would of course appreciate any feedback.