* Note. The following question is not intended for people, but is asked in terms of the best processing speed for a web page, jQuery, etc.
I currently have code that follows the following "test" code:
$(document).ready(function() { $('.my-class').on('click') { if ($('.my-class').hasClass('active') { $('.my-class').removeClass('active'); return; } $('.my-class').addClass('active'); } });
My question is: should the event handler (and not the event listener) be in the same code structure as $(document).ready(); ? Or it should look like this:
function toggler(obj) { if ($(obj).hasClass('active') { $(obj).removeClass('active'); return; } $(obj).addClass('active'); } $(document).ready(function() { $('.my-class').on('click') { toggler(this); } });
i.e. should $(document).ready(); have only listeners that reference handlers, or all actions (listening and processing) are in $(document).ready();
What is the right way to do this to maximize usability / jQuery, JS, etc.
source share