Skip to main content
Participant
October 23, 2019
Question

Set checkbox values based on other field values using On Focus action to run javascript

  • October 23, 2019
  • 1 reply
  • 1107 views

I'm trying to write a script to populate checkbox values based on the values in some hidden text fields and use the On Focus action to run the script when a user clicks an icon at the top of the document. I'm not sure what's wrong with the script but instead of only checking the boxes based on the values of the related fields, it's setting the values of the fields to be the value required for the boxes to be checked. In the example below when I click the icon where I have the script set to run On Focus, it checks all the boxes AND populates the text fields with the values I'm using in the conditional statements, for example it sets Mobil_Unsteady_text = 2 even though it was blank in the document until the script ran. The document is being downloaded from a website and the text fields are populated with the values associated with the record in that database and I want to check the boxes based on those values. What am I missing? I've tried the script below without an else statement, too (just an if then with the value and the "on" for the check boxes).

 

if (this.getField("Mobil_Unsteady_text").value="") this.getField("Mobil_Unsteady").value = "Off";
else if (this.getField("Mobil_Unsteady_text").value="2") this.getField("Mobil_Unsteady").value = "On";
if (this.getField("Mobil_WalkbySelf_text").value="") this.getField("Mobil_WalkbySelf").value = "Off";
else if (this.getField("Mobil_WalkbySelf_text").value="7") this.getField("Mobil_WalkbySelf").value = "On";
if (this.getField("Mobil_WalkwObject_text").value="") this.getField("Mobil_WalkwObject").value = "Off";
else if (this.getField("Mobil_WalkwObject_text").value="8") this.getField("Mobil_WalkwObject").value = "On";
if (this.getField("Mobil_WalkwAssistPerson_text").value="") this.getField("Mobil_WalkwAssistPerson").value = "Off";
else if (this.getField("Mobil_WalkwAssistPerson_text").value="9") this.getField("Mobil_WalkwAssistPerson").value = "On";
if (this.getField("Mobil_NeedHoya_text").value="") this.getField("Mobil_NeedHoya").value = "Off";
else if (this.getField("Mobil_NeedHoya_text").value="10") this.getField("Mobil_NeedHoya").value = "On";

    This topic has been closed for replies.

    1 reply

    Bernd Alheit
    Community Expert
    Community Expert
    October 23, 2019

    To compare values you must use ==, not only one =

     

    if (this.getField("Mobil_Unsteady_text").value=="") ...

    emegninAuthor
    Participant
    October 23, 2019

    Thanks. I tried that and now the script is not populating any check boxes when the condition is met. Do I need the else statement? Am I missing something else? I've also cleaned up the formatting a bit.

    if (this.getField("Mobil_Unsteady_text").value == "2") {
    this.getField("Mobil_Unsteady").value = "On";
    }
    if (this.getField("Mobil_Independent_text").value == "3") {
    this.getField("Mobil_Independent").value = "On";
    }
    if (this.getField("Mobil_WheelchairwAssist_text").value == "4") {
    this.getField("Mobil_WheelchairwAssist").value = "On";
    }
    if (this.getField("Mobil_WheelchairDependent_text").value == "5") {
    this.getField("Mobil_WheelchairDependent").value = "On";
    }
    if (this.getField("Mobil_AssistNeededAll_text").value == "6") {
    this.getField("Mobil_AssistNeededAll").value = "On";
    }
    if (this.getField("Mobil_WalkbySelf_text").value == "7") {
    this.getField("Mobil_WalkbySelf").value = "On";
    }
    if (this.getField("Mobil_WalkwObject_text").value == "8") {
    this.getField("Mobil_WalkwObject").value = "On";
    }
    if (this.getField("Mobil_WalkwAssistPerson_text").value == "9") {
    this.getField("Mobil_WalkwAssistPerson").value = "On";
    }
    if (this.getField("Mobil_NeedHoya_text").value == "10") {
    this.getField("Mobil_NeedHoya").value = "On";
    }

    Bernd Alheit
    Community Expert
    Community Expert
    October 23, 2019

    Can you share the form?