I have a script that enhances a button so the page can return to top and then open a sidebar. It contains script that allows a button 'reset' as it is an on click script. The reset part only works once after a page refresh and will not work after that. The bottom part 'toggleClick(true);' is the part that only works once. How can I make it so the reset part works consistently?
jQuery(document).ready(function($) {
function toggleClick(on) {
if (on) {
$('.button').on('click', function() {
setTimeout(function() {
$('.sidebaropen')[0].click()
}, 210);
$("html, body").animate({
scrollTop: 0
}, "fast");
toggleClick(false);
console.log('clicked');
});
} else {
$('.button').off('click');
}
}
toggleClick(true);
$('.button').on('click', function() {
console.log('Reset done');
setTimeout(function() {
toggleClick(true);
}, 210);
});
});