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

Adding/Subtracting Current Value from Form Field

Engaged ,
Nov 07, 2018 Nov 07, 2018

Copy link to clipboard

Copied

I'd like to change the current values of 12 numerical text fields on the mouseup of a button. I've tried defining variables and saying the variable value equals itself + or - the change, creating a temporary variable and assigning a value to it and systematically changing the values of each, creating  functions, etc. Here's my script worksheet with everything I've tried. the last has a document level function. The simple "variable = variable + number" almost works but it only seems to apply the math to the last line and not the other three...lines 17-20 do this...ignore all the note markup when analyzing code, that's only so I didn't have to delete things that didn't work.

What am I missing?

/*

var j9opi1 = this.getField("j9-opi1").value;

var j9opi2 = this.getField("j9-opi2").value;

var j9opi3 = this.getField("j9-opi3").value;

var j9opi4 = this.getField("j9-opi4").value;

var j9opi5 = this.getField("j9-opi5").value;

var j9opi6 = this.getField("j9-opi6").value;

var j9opi7 = this.getField("j9-opi7").value;

var j9opi8 = this.getField("j9-opi8").value;

var j9opi9 = this.getField("j9-opi9").value;

var j9opi10 = this.getField("j9-opi10").value;

var j9opi11 = this.getField("j9-opi11").value;

var j9opi12 = this.getField("j9-opi12").value;

j9opi2 = j9opi2 - 9;

j9opi3 = j9opi3 - 9;

j9opi4 = j9opi4 + 2;

j9opi5 = j9opi5 - 4;

*/

var f1 = this.getField("j9-opi1");

var f2 = this.getField("j9-opi2");

var f3 = this.getField("j9-opi3");

var f4 = this.getField("j9-opi4");

var f5 = this.getField("j9-opi5");

var f6 = this.getField("j9-opi6");

var f7 = this.getField("j9-opi7");

var f8 = this.getField("j9-opi8");

var f9 = this.getField("j9-opi9");

var f10 = this.getField("j9-opi10");

var f11 = this.getField("j9-opi11");

var f12 = this.getField("j9-opi12");

var j9pins = this.getField("j9-pins1");

var j9out = this.getField("j9-out1");

var temp = 0;

/*

j9pins.display = display.hidden;

j9out.display = display.visible;

temp.value = f11;

f11 = f5;

f5 = f9;

f9 = f8;

f8 = f7;

f7 = f3;

f3 = f12;

f12 = f6;

f6 = f10;

f10 = f4;

f4 = f2;

f2 = temp.value;

function sortOut(a,b) {

    a.value = b.value;

}

//temp = f2.value;

sortOut(f2,f4);

sortOut(f4,f10);

sortOut(f10,f6);

sortOut(f6,f12);

sortOut(f12,f3);

sortOut(f3,f7);

sortOut(f7,f8);

sortOut(f8,f9);

sortOut(f9,f5);

sortOut(f5,f11);

//f11.value = temp;

*/

/*

((f2.value = f2.value - 9) &&

(f3.value = f3.value - 9) &&

(f4.value = f4.value + 2) &&

(f5.value = f5.value - 4));

var f2a = f2.valueAsString;

var f3a = f3.valueAsString;

var f4a = f3.valueAsString;

var f5a = f4.valueAsString;

sortOutS(f2,9);

sortOutS(f3,9);

sortOutA(f4,2);

sortOutS(f5,4);

*/

I'm currently developing an electrical schematic that has an output connector with 12 pins. The pins and outputs are not in chronological order from each other, so a feature has been requested to allow the end user to sort by either pin or by output. There isn't a pattern.

Furthermore, the form has 23 outputs that can be grouped in groups of 20. (1-23, 21,43, etc up to 203). For that, I wrote a script to change the value of each field by by creating a variable with a value that adds to the default value as a custom keystroke script.

var out = this.getField("outputs");

var add = 0;

if(out.value == "1-23") {

add = 0;

}

else if(out.value == "21-43") {

add = 20;

}

else if(out.value == "41-63") {

add = 40;

}

else if(out.value == "61-83") {

add = 60;

}

else if(out.value == "81-103") {

add = 80;

}

else if(out.value == "101-123") {

add = 100;

}

else if(out.value == "121-143") {

add = 120;

}

else if(out.value == "141-163") {

add = 140;

}

else if(out.value == "161-183") {

add = 160;

}

else if(out.value == "181-203") {

add = 180;

}

for (var i = 0; i < this.numFields; i++) {

    var f = this.getField(this.getNthFieldName(i));

    var reg = /.+-opi\d+/;

    if(f.name.match(reg)) {

        f.value = f.defaultValue + add;

    }

}

TOPICS
Acrobat SDK and JavaScript , Windows

Views

815

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 , Nov 07, 2018 Nov 07, 2018

Check the JS Console for error messages (Ctrl+J).

Also, I recommend converting the values to a Number explicitly, like this:

f2.value = Number(f2.value) - 9; 

f3.value = Number(f3.value) - 9; 

f4.value = Number(f4.value) + 2; 

f5.value = Number(f5.value) - 4;

Votes

Translate

Translate
Community Expert ,
Nov 07, 2018 Nov 07, 2018

Copy link to clipboard

Copied

If you want to change a field's value you have to do so directly. If you save the value to a variable and then change the variable, it will not change the actual field. For example:

var v = this.getField("A").value;

v = 120; // This DOES NOT change the value of field A

-------------------------------------------------------

var f = this.getField("A");

f.value = 120; // This DOES change the value of field A

-------------------------------------------------------

var f = this.getField("A");

var v = 120;

f.value = v; // This DOES change the value of field A

-------------------------------------------------------

var v = 120;

this.getField("A").value = v; // This DOES change the value of field A

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
Engaged ,
Nov 07, 2018 Nov 07, 2018

Copy link to clipboard

Copied

Thanks, I'm not sure where I was doing that but the code must add/subtract from its current value. I've tried defining the field variables and then saying the variable.value equals itself and the math. No matter what, the last line works and the others don't. In other words, if any of these lines are run by themselves it works, but they don't work when run together.

var f2 = this.getField("f2");

var f3 = this.getField("f3");

var f4 = this.getField("f4");

var f5 = this.getField("f5");

f2.value = f2.value - 9;

f3.value = f3.value - 9;

f4.value = f4.value + 2;

f5.value = f5.value - 4;

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 ,
Nov 07, 2018 Nov 07, 2018

Copy link to clipboard

Copied

Check the JS Console for error messages (Ctrl+J).

Also, I recommend converting the values to a Number explicitly, like this:

f2.value = Number(f2.value) - 9; 

f3.value = Number(f3.value) - 9; 

f4.value = Number(f4.value) + 2; 

f5.value = Number(f5.value) - 4;

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 ,
Nov 07, 2018 Nov 07, 2018

Copy link to clipboard

Copied

If you're running the code form the console window, make sure to select all of it before pressing Ctrl+Enter.

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
Engaged ,
Nov 07, 2018 Nov 07, 2018

Copy link to clipboard

Copied

I modified code as suggested and it's still doing the same thing.

The console window is returning 13 which is correct for the value of f5-4—it's just doesn't seem to want to run each separately.

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
Engaged ,
Nov 07, 2018 Nov 07, 2018

Copy link to clipboard

Copied

LATEST

Figured out the issue. The console didn't show it but I was originally running the for loop in the calculation tab of a combobox. I had switched to a custom keystroke script and apparently hadn't removed the calculation script.

I switched changed the read-only property of the fields and tried to change the field value to find that the combobox was trying to run a calculation script!

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