Search code examples
window.open

trouble with document.getElementById().onclick


I need to create a T&C's splash page whereby the user ticks the box and then the submit button enables and forwards them onto the correct page.

Currently I cannot figure out why the following code makes the terms checkbox the active link - clicking on it to check the box loads the page from window.open

If I comment out the two window.open lines it all works as intended but without link function. Clearly I am trying to create a page that works on all mobile and desktop clients otherwise I would use an inline onclick in the button html.

Thanks for your help...

<script>
 function disableSubmit() {
  document.getElementById("submit").disabled = true;
 }

  function activateBox(element) {

      if(element.checked) {
        document.getElementById("submit").disabled = false;
		document.getElementById("submit").value = "Press here to go online";
		document.getElementById("submit").onclick= window.open('<?php print $grant_url ?>');
		document.getElementById("submit").ontouchstart= window.open('<?php print $grant_url ?>');
       }
       else  {
        document.getElementById("submit").disabled = true;
		document.getElementById("submit").value = "Please agree to T&C's";
      }

  }; 
</script>
<form>
      <input type="checkbox" name="terms" id="terms" onchange="activateBox(this)" autofocus />
      I Agree to the Terms & Conditions <br>
  
      <input class="button" name="submit" value="Please agree to T&C's" id="submit" title="Continue to the Internet" />
</form>


Solution

  • This should work (wrapping the window.open calls in anonymous functions)

    <script>
     function disableSubmit() {
      document.getElementById("submit").disabled = true;
     }
    
      function activateBox(element) {
    
          if(element.checked) {
            document.getElementById("submit").disabled = false;
            document.getElementById("submit").value = "Press here to go online";
            document.getElementById("submit").onclick= function() { window.open('<?php print $grant_url ?>'); }
            document.getElementById("submit").ontouchstart= function() { window.open('<?php print $grant_url ?>'); }
           }
           else  {
            document.getElementById("submit").disabled = true;
            document.getElementById("submit").value = "Please agree to T&C's";
          }
    
      }; 
    </script>