Skip to main content
Participant
June 27, 2017
Answered

How to delete contents of a form field when it becomes hidden

  • June 27, 2017
  • 2 replies
  • 1361 views

I've got a basic table in my pdf that looks like this:

SelectionCategoryAmount
<Category 1 check box>Category 1<Category 1 amount>
<Category 2 check box>Category 2<Category 2 amount>
<Category 3 check box>Category 3<Category 3 amount>
Total<calculated form field (sum of above)>

I've used a basic Mouse up javascript on each of the check boxes so that the user can't enter an amount unless they've checked the appropriate box (see below):

// Determine if Check box is marked then use value to show or hide appropriate field

var nHide = event.target.isBoxChecked(0)?display.visible:display.hidden;

// Get the appropriate field and set to show/hide value

this.getField("Category 1 amount").display = nHide;

This set up works great, but has one problem. If someone selects a check box and then puts an amount in, and then later unchecks that box, the total still sums up the value even though it's hidden.

So for example, the user clicks the category 1 check box and then types $10 in the Category 1 amount box. Then they do the same thing for Category 3. The total now reads $20. Great! Except the user now realizes they meant to click category 2 instead of category 3. So they uncheck category 3, click category 2, and enter $10 in category 2. Now the total reads $30 because it's summing the value of all the boxes, even the hidden ones.

My question is this. Is it possible to modify or make a new script that clears the values in the Amount column if the corresponding Selection column check box is unchecked?

This topic has been closed for replies.
Correct answer try67

Summing up just visible fields:

var fields = ["Field1", "Field2", "Field3"];

var total = 0;

for (var i in fields) {

    var f = this.getField(fields);

    if (f.display==display.visible) total+=Number(f.value);
}

event.value = total;

2 replies

Inspiring
June 27, 2017

One can set the field to a null value or reset the specific field when the box is checked. Then the value will be treated like zero. Unfortunately the value of the field is lost, so if the check box is unchecked the value would have to be reestablished.

Bernd Alheit
Community Expert
Community Expert
June 27, 2017

You can calculate the sum of the visible fields.

Participant
June 27, 2017

That sounds like the solution. So any pointers on how to sum visible values only? Would I need to run a custom calculation script?

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 27, 2017

Summing up just visible fields:

var fields = ["Field1", "Field2", "Field3"];

var total = 0;

for (var i in fields) {

    var f = this.getField(fields);

    if (f.display==display.visible) total+=Number(f.value);
}

event.value = total;