Based on checkbox response, populate text field with text from another field
Copy link to clipboard
Copied
I have a checkbox and a text field.
If I check the box, I want the text field beside it to populate with the first, middle, and last name that has been entered in the form.
I don't want the user to enter any text (name) they want here, rather I want the text field to display the name that they already entered prior to getting to this question.
How do I do this?
Thanks.
Copy link to clipboard
Copied
In your checkbox mouse-down event, you can copy the data from other fields and assign it to the target field. Or, you can craete a calculation event for the field that first checks the checkbox status, and if it is checked, copies the data from the other fields. As long as you make the field read-only, it will only be populated by the callback function, and the user cannot override the value.
Here is what you could do as the calculation script for the field:
// is the checkbox checked or not?
if (this.getField("CheckBox").value == "Off") {
event.value = "";
}
else {
event.value = this.getField("firstName").value + " " +
this.getField("middleName").value + " " +
this.getField("lastName").value;
}
Copy link to clipboard
Copied
Hi thank you for this - I had a follow up question. I am trying to do something very similar and was able to use your code to work perfectly for 1 checkbox and 1 textbox. However, how do you set this up with multiple checkbox options but only 1 textbox.
Can I use else if statements to achieve this? I have tried to write something on my own but it doesnt work. Here it is so far, but I am extremely new to this.
if (this.getField("Checkbox1").value == "ON") {
event.value = "Address for checkbox1 location";
}
else if (this.getField("Checkbox2").value == "ON") {
event.value = "Address for checkbox2 location";
}
else if (this.getField("Checkbox3").value == "ON"){
event.value = "Address for checkbox3 location";
} else {
event.value = "";
}
Copy link to clipboard
Copied
You are on the right track, however there are a couple of things that make this not as straight forward as you probably wish it would be. Firsts of all, the two states of a checkbox are "Off" and then something that can be configured, and it is not "ON" by default: Without any overrides, a checked checkbox will report "Yes", so it's "Off" and "Yes" - yes, I know, it does not make much sense 🙂 You can override what the checked checkbox reports by going to the "Options" tab of the field properties dialog and then change the Export Value (to e.g. "ON" to make your script work).
Because of this uncertainty regarding the checked state, I always test for "not Off". This would make your script look like this:
if (this.getField("Checkbox1").value != "Off") {
event.value = "Address for checkbox1 location";
}
else if (this.getField("Checkbox2").value != "Off") {
event.value = "Address for checkbox2 location";
}
else if (this.getField("Checkbox3").value != "Off"){
event.value = "Address for checkbox3 location";
} else {
event.value = "";
}
This will make it work - unless you have two or more checkboxes checked. Then there is a priority to which string gets used to populate the text field: The first test that yields a valid case will set the text, and then the comparisons will stop.
To avoid that, and if you can make sure that only one checkbox should be checked at any given time, you can use radio buttons. They behave almost like checkboxes, but only one of them can be active. You would assign an export value to each radio button, and then test for that in your if/else construct.
You can also use a checkbox group with very similar behavior to radio buttrons, but the look of checkboxes: Just name all checkboxes with the same name, but use again different export values for the three checkboxes. This approach has the advantage that you can acutally turn all of them off again (which you cannot do with radio buttons).
Here is the script that would work with either radio buttons, or a checkbox group (assuming that the export values are 1, 2 and 3 and that the checkbox group is called CheckboxGroup):
if (this.getField("CheckboxGroup").value == "1") {
event.value = "Address for checkbox1 location";
}
else if (this.getField("CheckboxGroup").value == "2") {
event.value = "Address for checkbox2 location";
}
else if (this.getField("CheckboxGroup").value == "3"){
event.value = "Address for checkbox3 location";
} else {
event.value = "";
}

