Search code examples
jquerycheckboxradio-buttonautofill

Pre-Fill a Checkbox or Radio Field from a Page Link Selected from Another Page?


So I've been having an unfulfilling experience trying to figure out how I can auto-fill/select a checkbox or radio field from a designated link on another page.

Thus far, I have figured out how to select certain input fields when clicking a directed link on the form's page itself with the following function/HTML:

The script:

enter image description here

And the HTML (on the form's page itself):

<a href='' id='select-checkbox1'>Click to Select</a>

Is there a way to activate a function like this automatically via page load, (on the form's page), initially prompted by a link on another page?

Then I was also thinking that maybe a field could be auto-filled when in correspondence with a specific hash target/anchor called in a URL. Any ideas?

Assistance would be very much appreciated, as I have limited expertise with Javascript/JQuery...

Thank you.


Solution

  • It is possible to carry the information from page A to page B using a hash.

    That is if you don't want to use a server-side resource.

    Page A:

    <a href='pageB.html#myHash' id='select-checkbox1'>Click to Select</a>
    

    Page B:

    $(document).ready(function(){
      var hash = location.hash;  // Here, it would be "#myHash"... So including the #
    
      if(hash=="#myHash"){
        $("#checkbox1").prop("checked", !$("#checkbox1").prop("checked") );  // that toggles the checked state
      }
    });