document.addEventListener('DOMContentLoaded', () => { var checkElList = document.getElementsByClassName('check-el'); var linkHandler = document.getElementById('link-handler'); var checkValue = 3; var url = "https://google.com" function handler(el) { el.addEventListener("click", () => { if (el.checked == true ) { counter ++; } else if (counter > 0) { counter --; } if (counter >= checkValue) { if (linkHandler && !linkHandler.classList.contains('link--active')) { linkHandler.classList.add('link--active'); setTimeout(() => { window.location = url; }, 5000); } } else if (linkHandler.classList.contains('link--active')) { linkHandler.classList.remove('link--active') } }); } if (checkElList && checkElList.length > 0) { checkValue = checkElList.length; var counter = 0; for (const el of checkElList) { el.addEventListener("click", handler(el)); } }; if (linkHandler) { linkHandler.addEventListener("click", (evt) => { evt.preventDefault(); if (counter >= checkValue) { window.location = url; // do something } }); } });