Javascript calculation after values entered.

Community Beginner ,
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; })();

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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 = "";

      

})();

TOPICS
Acrobat SDK and JavaScript

Views

523

Likes

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

Javascript calculation after values entered.

Community Beginner ,
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; })();

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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 = "";

      

})();

TOPICS
Acrobat SDK and JavaScript

Views

524

Likes

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
Aug 23, 2017 0
Most Valuable Participant ,
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?

Likes

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
Reply
Loading...
Aug 23, 2017 0
Community Beginner ,
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.

Likes

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
Reply
Loading...
Aug 23, 2017 0
Most Valuable Participant ,
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 = "";

      

})();

Likes

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
Reply
Loading...
Aug 23, 2017 0
Community Beginner ,
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.

Likes

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
Reply
Loading...
Aug 23, 2017 0
try67 LATEST
Most Valuable Participant ,
Aug 23, 2017

Copy link to clipboard

Copied

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

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

Likes

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
Reply
Loading...
Aug 23, 2017 0