• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Betreff: I want to dynamically change a part (numb...

# I want to dynamically change a part (number) in the object name in the code.

Mar 15, 2020 Mar 15, 2020

Copied

I am a complete beginner.
This is a very rudimentary question.

I am creating a slightly spreadsheet-like form.

Objectize each field as follows.

Intuitively, A, B and C are vertical columns.
Think of 12345 as a row.

var A1 = this.getField("A_1");
var A2 = this.getField("A_2");
var A3 = this.getField("A_3");
var A4 = this.getField("A_4");
var A5 = this.getField("A_5");

var B1 = this.getField("B_1");
var B2 = this.getField("B_2");
var B3 = this.getField("B_3");
var B4 = this.getField("B_4");
var B5 = this.getField("B_5");

var C1 = this.getField("C_1");
var C2 = this.getField("C_2");
var C3 = this.getField("C_3");
var C4 = this.getField("C_4");
var C5 = this.getField("C_5");

I am trying to copy the value entered in the specified C field to B based on the value entered in A.

Each row has a button, and the following JavaScript is embedded in the button.

Very simple code.

if(A1.value == 3000){B1.value = C1.value;}
if(A1.value == 6000){B1.value = C2.value;}
if(A1.value == 12000){B1.value = C3.value;}
if(A1.value == 24000){B1.value = C4.value;}
if(A1.value == 36000){B1.value = C5.value;};

The problem is that I have to place this code on a button on every line.
The second line looks like this.

if(A2.value == 3000){B2.value = C1.value;}
if(A2.value == 6000){B2.value = C2.value;}
if(A2.value == 12000){B2.value = C3.value;}
if(A2.value == 24000){B2.value = C4.value;}
if(A2.value == 36000){B2.value = C5.value;};

The third line looks like this.

if(A3.value == 3000){B3.value = C1.value;}
if(A3.value == 6000){B3.value = C2.value;}
if(A3.value == 12000){B3.value = C3.value;}
if(A3.value == 24000){B3.value = C4.value;}
if(A3.value == 36000){B3.value = C5.value;};

This is very time consuming when writing code...
I want to make about 300 buttons like this.

In the above example, the numbers must be rewritten 10 times per line.
I want to do this with one rewrite.

Isn't it possible to assign the number of the object name indicating the line in any way?

I tried various things, but none worked well.

TOPICS
Acrobat SDK and JavaScript

Views

1.2K

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 Expert , Mar 17, 2020 Mar 17, 2020

Don't use a fix value for i. You can ectract the value from the name of the button.

10 Replies 10
Community Expert ,
Mar 16, 2020 Mar 16, 2020

Copied

What names does you use for the buttons?

Report

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

Copied

The button field names are as follows...

"MB_1"
"MB_2"
"MB_3"
"MB_4"
"MB_5"

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 Expert ,
Mar 16, 2020 Mar 16, 2020

Copied

At the button you will get the row number with:

Number(event.target.name.substring(3))

You can use this in a document level function. At every button use this function.

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 Expert ,
Mar 16, 2020 Mar 16, 2020

Copied

This code can be used to replace all the code you have, and if you want to apply it to 300 fields instead of 5 just change the value of maxFields in line #2:

``````var values = [0, 3000, 6000, 12000, 24000, 36000];
var maxFields = 5;
for (var i=1; i<=maxFields; i++) {
var A = this.getField("A"+i);
var B = this.getField("B"+i);
var idx = values.indexOf(Number(A.valueAsString));

// in case of no-match or if the A field is empty reset B
if (idx<=0) B.value = "";
// in case of a match copy the value of the relevant C field to B
else B.value = this.getField("C"+idx).value;
}
``````

Report

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

Copied

I am very happy.

I think the code you gave is very close to what I want.
But, unfortunately, there is a slight misunderstanding.

Maybe I didn't have enough words in the text of my first question.
I am Japanese and not good at English. Sorry..

===

I want to make it easier to write code in each button in a form.
Because I needs to create nearly 300 buttons.

① These five lines of code are described in one button field.

A1 and B1 remain the same in all five lines.

if (A1.value == 3000) {B1.value = C1.value;}
if (A1.value == 6000) {B1.value = C2.value;}
if (A1.value == 12000) {B1.value = C3.value;}
if (A1.value == 24000) {B1.value = C4.value;}
if (A1.value == 36000) {B1.value = C5.value;};

② Every row in a form that is structured like a spreadsheet has a button for each.

The above code is the button on the first row when the whole form is viewed like a spreadsheet.
And the above code is written on that 1st button. (Row 1)

There are 300 row, so there are 300 buttons.

③Basically, when I make new button, copy and paste code.

Then rewrite only the necessary parts.

The code for the second button on the second row must change.

A1 becomes to A2.
B1 becomes to B2.

The button on the 300th row will be A300 and B300.

④As it is, I must write the number next of A five times for each button until the button on the 300th row is created. And also 5 five times next to B.

In the 300th button

if (A300.value == 3000) {B300.value = C1.value;}
if (A300.value == 6000) {B300.value = C2.value;}
if (A300.value == 12000) {B300.value = C3.value;}
if (A300.value == 24000) {B300.value = C4.value;}
if (A300.value == 36000) {B300.value = C5.value;};

⑤ In other words, I have to 300 times rewrite the numbers next to A and B in the five lines of code.

I want to make this job to easier.

For example, I wrote the following code.

var i = 300
if (("A" + "i"). value == 3000) {("B" + "i"). value = C1.value;}
if (("A" + "i"). value == 6000) {("B" + "i"). value = C2.value;}
if (("A" + "i"). value == 12000) {("B" + "i"). value = C3.value;}
if (("A" + "i"). value == 24000) {("B" + "i"). value = C4.value;}
if (("A" + "i"). value == 36000) {("B" + "i"). value = C5.value;};

In this case, changes in each button can be made in only one place, greatly simplifying the work.

But this code doesn't work.

I want you to teach me how to write this correctly.

Thankyou and Best Regards.

Report

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

Copied

By the way, the following may be unnecessary information.

The value of assigned to C1, C2, C3, C4, C5 are changed before use this button.

Under different code of different field of each row .

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 Expert ,
Mar 16, 2020 Mar 16, 2020

Copied

Use

this.getField ("A_" + i).value

and

this.getField ("B_" + i).value

Report

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

Copied

To Mr. or Ms. Bernd_Alheit.
And also Mr. or Ms. try67.

Thankyou !!!
God bless to you !!!

I am really stupid.
I did not understand how to use <i> basically.

I succeeded by writing the code below!

var i = 2

var Aa = this.getField("A_" + i);
var Bb = this.getField("B_" + i);

if (Aa.value == 3000) {Ba.value = C1.value;}
if (Aa.value == 6000) {Ba.value = C2.value;}
if (Aa.value == 12000) {Ba.value = C3.value;}
if (Aa.value == 24000) {Ba.value = C4.value;}
if (Aa.value == 36000) {Ba.value = C5.value;};

In any case, thank you very much!
You are my savior!

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 Expert ,
Mar 17, 2020 Mar 17, 2020

Copied

Don't use a fix value for i. You can ectract the value from the name of the button.

Report

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

Copied

LATEST

Mr.Bernd_Alheit.

I should use a syntax of "for ".

I must to learn each of each and make it my own knowledge.

I'll do my best.

Please do not hesitate to give me advice in the next question.

Thank you.