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

"Undefined" and "Off" result when radio button not selected

407chelsie
Registered: Dec 8 2008
Posts: 68
Answered

My calculation works great, BUT when I clear the form, I get and "Undefined" in one field and and "Off" in the other. Here's the framework of the problem:

Radio buttons:
( ) Standard Membership [i]Export Value "145:25"[/i]
( ) Education Membership [i]Export Value "75:0"[/i]
( ) Student Membership [i]Export Value "35:0"[/i]
( ) Government Membership [i]Export Value "75:0"[/i]

Application Fee: __[u]undefined[/u]__

var mem_class = this.getField("Membership Classification").value;
 
// split Membership Classification value
var app_fee = mem_class.split(":");
this.getField("Application Fee").value = app_fee[1];

Membership Dues: __[u]Off[/u]__
var mem_class = this.getField("Membership Classification").value;
 
// split Membership Classification value
var mem_dues = mem_class.split(":");
this.getField("Membership Dues").value = mem_dues[0];

Total _____ [i]adds App Fee and Mem Dues[/i]

How do I get rid of "undefined" and "Off" and have the field blank until a radio button is selected?

My Product Information:
Acrobat Pro 8.1.2, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
I'm assuming this is a custom calculation script for the Application Fee field. If so the code could look like:

var mem_class = this.getField("Membership Classification").value; // split Membership Classification valueif (mem_classs !== "Off") {var app_fee = mem_class.split(":");event.value = app_fee[1];} else {event.value = "";}

Similarly, the other could be:

var mem_class = this.getField("Membership Classification").value; if (mem_class !== "Off") {// split Membership Classification valuevar mem_dues = mem_class.split(":");event.value = mem_dues[0];} else {event.value = "";}

I hope this makes sense.

George
407chelsie
Registered: Dec 8 2008
Posts: 68
Thanks for responding George. As a novice, I love that your code is simple enough that I truly understand what they're saying. Which is why I don't understand why I got this unexpected result:

When I update the two scripts, the calculation no longer functions. I get nothing in the Application Fee field, I get the Membership Dues, but the Total looks like it just added a "0". For example, the first radio button shows nothing in the App Fee field, "145" in the Membership Dues field and "1450" in the Total field. I am posting [url=http://idisk.mac.com:80//chelsie.hall/Public/App-RM-09117-4.pdf]the original form here[/url].

Thank you for your help.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Hmm...it works for me, but the other two fields have some questionable code. Here's is what I would use as the custom calculation scripts for the four fields:

/*********** belongs to: AcroForm:Application Fee:Calculate ***********/(function () { var mem_class = getField("Membership Classification").value; if (mem_class !== "Off") {var app_fee = mem_class.split(":");event.value = app_fee[1];} else {event.value = "";} })(); /*********** belongs to: AcroForm:Membership Dues:Calculate ***********/(function () { var mem_class = getField("Membership Classification").value; if (mem_class !== "Off") {var mem_dues = mem_class.split(":");event.value = mem_dues[0];} else {event.value = "";} })(); /*********** belongs to: AcroForm:Specialty Membership Total:Calculate ***********/(function () { var ceesp = getField("Chief Audit Executive Services Program").value;var fsag = getField("Financial Services Auditor Group").value;var gag = getField("Gaming Audit Group").value; if (ceesp === "Off") ceesp = 0;if (fsag === "Off") fsag = 0;if (gag === "Off") gag = 0; var sum = ceesp + fsag + gag; event.value = sum ? sum : ""; })(); /*********** belongs to: AcroForm:Total:Calculate ***********/(function () { // Get field values, as numbersvar app_fee = +getField("Application Fee").value;var mem_dues = +getField("Membership Dues").value;var spec_mem = +getField("Specialty Membership Total").value; var sum = app_fee + mem_dues + spec_mem; event.value = sum ? sum : ""; })();

Make sure all the fields are set to visible, not hidden.

George
407chelsie
Registered: Dec 8 2008
Posts: 68
That worked! You are my hero!

I understand everything you did except:
• What is the leading and closing statement? [b](function () { … })();[/b]
• What does [b]sum ? sum : ""[/b] mean? Couldn't you just say [b]event.value = [i](calculation on the line above)[/i][/b]?

Thank you sooo much for your help. If you were local, I would bake you something!
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
The following:

(function () { // Code goes here })();

is something I almost always use for code that I place in field-level events, as opposed to document-level functions, though I use it in document-level code as well. What it does is sets up an anonymous function that calls itself. Variables defined in functions have scope that is limited to that function. Variables defined outside of functions have global scope, which means the variable is visible to code in other parts of the document, which is almost always unnecessary and can lead to hard to find bugs. So I use a coding practice that eliminates this problem. In my opinion (and in the opinion of many who are more proficient then I), you should not create global variables unless necessary, and it's almost never necessary. In the cases where it is, it is better to create an object and have the global variable be a property of that object. For example:

// Create an object to contain variables/function for this documentvar doc_obj = {}; // Define a variable that is in effect global but contained within the object// which minimizes the possibility that is will accidentally get overwritten or// misuseddoc_obj.myvar = "Hello"; // Similarly, define a function that won't clash with other code that may be presentdoc_obj.myfunc = function  () { app.alert("Hello", 3); };

For your second question, that is known as the tertiary operator in JavaScript. The following are equivalent:

// This code is equivalent...if (sum <> 0) {event.value = sum;} else {event.value = "";}  // ...to this codeevent.value = sum ? sum : "";

So it's just a convenient shorthand. Any decent core JavaScript reference will tell you more.

George
407chelsie
Registered: Dec 8 2008
Posts: 68
Thank you so much. You make learning fun!