• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Javascript calculation after values entered.

Community Beginner ,
Aug 23, 2017 Aug 23, 2017

Copy link to clipboard

Copied

I currently have the following java script in a adobe pdf to perform calculations.  One of the problems I am running into is the form is performing the calculations live.  I do not want the calculations to be performed till values are entered.  So currently the 3 text boxes are "Neck1", "Neck2" and "Neck3".  Once values are entered into these boxes I would like to perform the average calculation.  Any help would be appreciated.  I am not experience with JavaScript so please be very detailed with answers.

// Custom calculation script (function () {

// Get the field values, as numbers

var v1 = +getField("Neck1").value;

var v2 = +getField("Neck2").value;

var v3 = +getField ("Neck3").value;

var nTotal = (v1 + v2 + v3)

var nAverage = (nTotal / 3)

var nRounded = Math.round(nAverage / .5)*.5;

event.value = nRounded; })();

TOPICS
Acrobat SDK and JavaScript

Views

905

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 23, 2017 Aug 23, 2017

Use this code:

(function () {

    var s1 = getField("Neck1").valueAsString;

    var s2 = getField("Neck2").valueAsString;

    var s3 = getField("Neck3").valueAsString;

    if (s1!="" && s2!="" && s3!="") {

        var v1 = +s1;

        var v2 = +s2;

        var v3 = +s3;

        var nTotal = (v1 + v2 + v3)

        var nAverage = (nTotal / 3)

        var nRounded = Math.round(nAverage / .5)*.5;

        event.value = nRounded;

    } else event.value = "";

      

})();

Votes

Translate

Translate
Community Expert ,
Aug 23, 2017 Aug 23, 2017

Copy link to clipboard

Copied

Do you mean that you don't want the calculation to occur until all three fields are non-empty?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 23, 2017 Aug 23, 2017

Copy link to clipboard

Copied

Yes.  That is correct.  I do not want the calculation to occur until all three fields are non-empty.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 23, 2017 Aug 23, 2017

Copy link to clipboard

Copied

Use this code:

(function () {

    var s1 = getField("Neck1").valueAsString;

    var s2 = getField("Neck2").valueAsString;

    var s3 = getField("Neck3").valueAsString;

    if (s1!="" && s2!="" && s3!="") {

        var v1 = +s1;

        var v2 = +s2;

        var v3 = +s3;

        var nTotal = (v1 + v2 + v3)

        var nAverage = (nTotal / 3)

        var nRounded = Math.round(nAverage / .5)*.5;

        event.value = nRounded;

    } else event.value = "";

      

})();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 23, 2017 Aug 23, 2017

Copy link to clipboard

Copied

That worked for a portion of the calculation.  I see in the code you sent me that

var s1 = getField("Neck1").valueAsString

is how to pull a value from a Field.  How do you move a value to a "Field"?

I currently have a bunch of small fields that contain calculations and these values field more calculations.  It seems to not be executing properly due to this.  I am wanting to form just one hug calculation and once the values are found then push the answers to multiple fields.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 23, 2017 Aug 23, 2017

Copy link to clipboard

Copied

LATEST

To apply a value of a field (not from its own calculation script), use this:

this.getField("FieldName").value = "New field value";

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines