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

Question

Question

Value copied to "Other" field doesn't persist in variable

asked on June 11, 2019 Show version history

I have a form with a checkbox field; there's an English version and a Spanish version, and whichever one the user fills out the values get copied over to a hidden third version (without getting too in depth, the reason for this is so users have an English or Spanish interface that includes several items in one paginated form, but I can then use the backend fields to save each page as a separate form in English). 

When I unhide the third version in the master paginated form to test it out, everything is working fine.  Checking a box in the English or Spanish version checks the corresponding box in the target field, and if I check 'Other' and enter text into the field that copies over too.  The problem is that when I submit the form and save the separate forms that have those hidden versions of the fields, the text that was entered into the 'Other' field is no longer there.  All other fields and checkboxes are correct, but going into the monitor and looking at that variable displays "Other checked but empty"

Here is the javascript that I'm using to copy the checkbox values over:
 

            var check1 = $('#q331 input[type=Checkbox][value=First_option]').is(':checked');
            var check2 = $('#q331 input[type=Checkbox][value=_other]').is(':checked');
            if (check1) {
                $('#q562 input[type=Checkbox][value=First_option]').prop("checked", true);
            } else {
$('#q562 input[type=Checkbox][value=First_option]').prop("checked", false);
}
            if (check2) {
                $('#q562 input[type=Checkbox][value=_other]').prop('checked', true);
                $('#q562 input[type=text][name=OtherField562]').val($('#q331 input[type=text][name=OtherField331]').val());
            } else {
                $('#q562 input[type=Checkbox][value=_other]').prop("checked", false);
                $('#q562 input[type=text][name=OtherField562]').val(null);
            }

So for the 'Other' checkbox, the variable "check2" has value if the 'other' box in #q331 (source) is checked.  If "check2" has value then it will set the target check box's "other" field to 'checked' also, and then it will copy the contents of the 'other' text box from the source to the target.

Like I said before, this all works within the form, but as soon as I call that variable up in another form its like the 'other' box being checked was saved, but the text didn't get saved to the variable so it doesn't carry over.

That all sounds really confusing, so ask away if it didn't make sense, and if you know of a better way to save that text into the third variable let me know!

0 0

Answer

SELECTED ANSWER
replied on June 11, 2019

I was able to fix the issue by adding a change trigger for the source field in the document ready function.
 

$(document).ready(function() {
$('#q331').change(function() {
var check1 = $('#q331 input[type=Checkbox][value=First_option]').is(':checked');
var check2 = $('#q331 input[type=Checkbox][value=_other]').is(':checked');
if (check1) {
  $('#q562 input[type=Checkbox][value=First_option]').prop("checked", true);
} else {
  $('#q562 input[type=Checkbox][value=First_option]').prop("checked", false);
}
if (check2) {
  $('#q562 input[type=Checkbox][value=_other]').prop('checked', true);
  $('#q562 input[type=text][name=OtherField562]').val($('#q331 input[type=text][name=OtherField331]').val());
} else {
  $('#q562 input[type=Checkbox][value=_other]').prop("checked", false);
  $('#q562 input[type=text][name=OtherField562]').val(null);
}
});
$('#q331').trigger('change');
});

 

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.