Normal JavaScript:
// Get DOM objects ready var checkbox = document.querySelector('input[type="checkbox"]'), downloadButton = document.querySelector('a#download'); // Set up function to change URL function chooseUrl() { var url = checkbox.checked ? 'http://example.com/default_download' : 'http://example.com/secondary_download'; downloadButton.setAttribute('href', url); } // Change URL on checkbox value change checkbox.addEventListener('change', chooseUrl); // Run chooseUrl once to set initial value chooseUrl();
JSFiddle Demo
JavaScript + jQuery:
// Get jQuery-wrapped DOM objects ready var checkbox = $('input[type="checkbox"]'), downloadButton = $('a#download'); // Set up function to change URL function chooseUrl() { var url = checkbox.is(':checked') ? 'http://example.com/default_download' : 'http://example.com/secondary_download'; downloadButton.attr('href', url); } // Change URL on checkbox value change checkbox.on('change', chooseUrl); // Run chooseUrl once to set initial value chooseUrl();
JSFiddle Demo
source share