You are viewing limited content. For full access, please sign in.

Question

Question

Prefill Checkbox with selected value(s) via URL

asked on November 16, 2015

Similar to the post found Here using the URL to pass field information, I am looking for a way to do this for a checkbox.  

I have tried using just the variable name, but this fills in the OTHER field with my value. 

https://formsserver/Forms/TestingEmployeeChange?REASON_FOR_CHANGE=0 

Where REASON_FOR_CHANGE is the variable name for my checkbox and NEW_HIRE is option 0

I am looking for this:

Any help is much appreciated.  

Thanks,

Nate

0 0

Replies

replied on November 16, 2015 Show version history

Nate, here's something that should work.

Place a hidden single line field at the top of your form. Let's assume it is called "Hidden Reason" and has an id of #q1.

Then insert this snippet into the JavaScript/CSS section (you can ignore the first and last line if you already have them):

$(document).ready(function() {
    
  var reasons = $('#q1 input').val().split(",");
  $.each(reasons, function(index, value) {
    $("input[value='" + value + "']").attr('checked', true);
  });
  
});

Then structure your URL to look like this:

myServer/Forms/form/new/24?Hidden_Reason=New_Hire,Promotion,Dept._Change

The bolded stuff is the values you want, and they are separated by commas. When the form loads, the hidden field is populated, then the code snippet above iterates through each value, finds its checkbox and checks it:

 

If you also need to populate the "Other" option using the URL, you can use the expanded version of the snippet below:

$(document).ready(function() {
    
  var reasons = $('#q1 input').val().split(",");
  var otherInput = $("#Field2_other_value");
  $.each(reasons, function(index, value) {
    if (value.lastIndexOf("Other;", 0) === 0) {
      var otherReason = value.split("Other;")[1];
      $("#Field2_other").prop("checked", true);
      otherInput.attr({
        disabled: false,
        readonly: false
      }).val(otherReason);
    } else {
      $("input[value='" + value + "']").attr('checked', true);
    }
  });
  
});

The only differences here is that in your URL, the "Other" reason has to be separated using a semicolon instead of a comma, so it should look like this:

myServer/Forms/form/new/24?Hidden_Reason=New_Hire,Promotion,Dept._Change,Other;myreason

And inside the code snippet, you need to target the Other checkbox and its input field using their ids. In my case they're #Field2_other and #Field2_other_value.

If you need to include spaces in your "Other" reason, use %20. HTML URL Encoding Reference

2 0
replied on November 16, 2015

Why don't you just default the selection to NEW HIRE from the fields configuration?  Double click on the number that corresponds to the NEW HIRE label, then it should be highlighted.

 

example:

0 0
replied on November 16, 2015

Tommy,

I thought about this, but the form will be completed by many people for many different reasons.  

This specific process is such that a user fills out an application, once a candidate is selected, I am sending a link to the Hiring Manager to complete the process by filling out a Change Request Form (above).  In this scenario only, I would want the NEW_HIRE selected by default.  For any other case, I do NOT want this to be pre-selected because the user may not update it and it could be for another type of change.  

I've researched this and found a few ways to do it using [0], but that does not work with Forms for whatever reason.  

0 0
replied on October 12, 2017

Nate,

 

Did you ever find a solution to passing check box selections via a URL to forms?

 

Thanks

You are not allowed to follow up in this post.

Sign in to reply to this post.