Skip to main content
sw777
Known Participant
April 18, 2019
Answered

Force value using checkbox

  • April 18, 2019
  • 9 replies
  • 2183 views

Hoping someone can help with this.

I have a form with several rows of fields as follows (only showing row 1 below)...

"Unit_1" (dropdown), "Unit_1_Fee" (textbox, currency), "Unit_1_Var" (textbox, numeric), "Unit_1_Actual_Fee" (textbox, currency).

When a unit is selected in "Unit_1", the associated fee is shown in "Unit_1_Fee" which is a value retrieved from a Document JavaScript.

If "Unit_1_Var" is empty, the value in "Unit_1_Fee" is also displayed in "Unit_1_Actual_Fee".

If a "1" is entered into "Unit_1_Var", the value in "Unit_1_Actual_Fee" is "120" (an override value).

The following script is attached to the "Unit_1_Actual_Fee" field to achieve this...

if (this.getField("Unit_1").valueAsString!=this.getField("Unit_1").defaultValue) {

var altValue = Number(this.getField("Unit_1_Var").value) * 120;

    event.value = altValue>0 ? altValue : (Number(this.getField("Unit_1_Fee").value);

} else event.value = "";

What I'm hoping to do is to modify this to use a checkbox instead of the numeric field "Unit_1_Var".

Ideally, I would like to be able to get rid of the "Unit_1_Actual_Fee" field and just have the "Unit_1_Fee" value change from the lookup value to "120" if the checkbox is selected.

Hopefully this makes sense. Any help would be appreciated.

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Thom Parker

The "if" block I supplied only deals with the checkbox. So yes, some of your other code needs to be copied over to get the full effect.

Like this:

if (this.getField("Unit_1").valueAsString!=this.getField("Unit_1").defaultValue)

{

     if(this.getField("Unit_1_Check").value == "Off")

      {

           normal calculation

      }

      else

           event.value = 120;

}

else

      event.value = "";

9 replies

Thom Parker
Community Expert
Community Expert
April 18, 2019

Let's say the check box is named "Unit_1_Check". This could be your calculation code for "Unit_1_Fee".

if(this.getField("Unit_1_Check").value == "Off")

{

    normal calculation

}

else

   event.value = 120;

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
sw777
sw777Author
Known Participant
April 19, 2019

Thanks Thom. This works OK but the "Unit_1_Fee" remains populated with 120 even if there is no unit selected in "Unit_1".

I assume I need another line to check that the "Unit_1" field is populated, otherwise return a blank value in "Unit_1_Fee".

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
April 19, 2019

The "if" block I supplied only deals with the checkbox. So yes, some of your other code needs to be copied over to get the full effect.

Like this:

if (this.getField("Unit_1").valueAsString!=this.getField("Unit_1").defaultValue)

{

     if(this.getField("Unit_1_Check").value == "Off")

      {

           normal calculation

      }

      else

           event.value = 120;

}

else

      event.value = "";

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
April 18, 2019

The above code should work with a check-box field as well as a text field, since the default value of a check-box is (usually) "Off".

sw777
sw777Author
Known Participant
April 19, 2019

Can't seem to work this out. I'm a bit of a hack when it comes to coding, but I'm trying a few variations. :-)