These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Check Boxes

bbelich
Registered: Nov 29 2010
Posts: 17

I would like to clear a field when the check box is clear and populate a field when a check box is populated. How can I get this accomplished? do I need a java script?

Bob

My Product Information:
Acrobat Pro 10.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
It depends. What exactly do you want to populate the (text?) field with when the check box is selected?
bbelich
Registered: Nov 29 2010
Posts: 17
A dollar amount.
I have a check box, that when checked, I would like to populate a dollar amount to be populated in a field to be added to the total amount due.
If unchecked, I would like that field to change to zero, so that nothing is added to the total amount due.

Bob

bbelich
Registered: Nov 29 2010
Posts: 17
I have check box that I will populate the text box with 40.00
I have another that will be 65.00

I would also like to perform this same task with radio buttons.
Is that possible?

Bob

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
A simple way to implement this is to set the export value of the check box to the amount you want in the text field when it is checked. Then add the following JavaScript in the MouseUp event of the check box:

// MouseUp script for check box
var f = getField("corresponding_text_field");
f.value = event.target.value !== "Off" ? event.target.value : 0;

Replace "corresponing_text_field" with the actual name of the text field.


An alternative is to set up the text field as a calculated field. In this case, do not use any JavaScript for the check box and use the following as the custom Calculate script for the text field:

// Custom calculation script for text box
var f = getField("corresponding_checkbox");
event.value = (f.value !== "Off") ? f.value : 0;

Again, use the actual name of the check box in your script.

If you need to do this for a lot of fields, you should create a document-level JavaScript that can be called by all of the fields, like this:

function calcPrice(f_name) {

var f = getField(f_name);
event.value = (f.value !== "Off") ? f.value : 0;
}

And call it like this in the text field's Calculate event:

// Custom calculation script for text box
calcPrice("corresponding_checkbox_name_goes_here");


You can make even more general by coordinating the field naming so that the script can deduce the name of the check box based on the name of the field that's calling the function. That way, you don't have to pass the name of the corresponding check box.

bbelich
Registered: Nov 29 2010
Posts: 17
Thank You.
This seams to work.

However, when i open the document, the fields are populated with the correct amount but the boxes are not checked by default.

Is there a way to have these field set at zero until the check boxes are actually checked?

I used this script:

var f = getField("corresponding_text_field");
f.value = event.target.value !== "Off" ? event.target.value : 0;

Bob

bbelich
Registered: Nov 29 2010
Posts: 17
ok.

I used the other script and it seems to work correctly.

Is there a way to apply the same features to Radio Buttons?

Bob

saj
Registered: Oct 15 2008
Posts: 52
I have a question similar to this. I want to have a if/else calculation happen when the checkbox is clicked but a zero value when it's NOT clicked. So, when the checkbox is turned off the value is zero, but when it is selected it will then goes out and reads the page count (a different field) and places the calculation into the "Yearbook Proof Cost" Field (f2).

Here is my calculation I want to use, but can't figure out the rest:

var v1 = this.getField("PageCount").value;
var f2 = getField("Yearbook_Proof_Cost");

if (v1 > 50) f2.value = (v1 * 0.50);
else f2.value = 25;


Please help me.
thanks,
sara



George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Assuming this is a Mouse Up script for the check box, the code could be something like:

  1. // Mouse Up script for check box
  2. (function () {
  3.  
  4. var v1 = getField("PageCount").value;
  5. var f2 = getField("Yearbook_Proof_Cost");
  6.  
  7. if (event.target.value !== "Off") {
  8. if (v1 > 50) {
  9. f2.value = v1 * 0.50;
  10. } else {
  11. f2.value = 25;
  12. }
  13. } else {
  14. f2.value = 0;
  15. }
  16.  
  17. })();
saj
Registered: Oct 15 2008
Posts: 52
You are a life saver!!! THANK YOU!