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

How do you setup a javascript to auto enter form boxes from an array?

pmarshall61
Registered: Sep 5 2011
Posts: 2
Answered

I am trying to make a form that has rows of book title, isbn no, and price.
 
I have setup a combo box for the book title list. I have then setup text boxes in the same row for isbn and price. I want to autfill the isbn and price boxes corresponding to the book title entered.
 
I have set up the following array.
 
var oTitle = {
"French book": [ ["1234567890123","None"]],
"German book": [ ["9876543210123","None"]],
"Song book": [ ["1231234567890","None"]],
"Italian book":[ ["1239876543210","None"]]
};
 
When the book title is committed, it runs the following script that puts relevant isbn,price in the isbn box.
 
var lst = oTitle[event.value];
if( (lst != null) && (lst.length > 0) )
this.getField(cRowName + ".isbn").value = lst;
else
{
this.getField(cRowName + ".isbn").value = 0;
}
 
How do I split the isbn and price and put them in the correct boxes?
 
I am using acrobat 9 pro.

My Product Information:
Acrobat Pro 9.0, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
It's not clear to me why that object is set up like that, but you can try something like the following:

  1. // Set up object to associate title with corresponding ISBN and price
  2. var oTitle = {
  3. "French book" : ["1234567890123", "None"],
  4. "German book" : ["9876543210123", "None"],
  5. "Song book" : ["1231234567890", "None"],
  6. "Italian book" : ["1239876543210", "None"]
  7. };
So each object property is a two element array, the first element is the ISBN number and the second is the price.

You can then access them and set the field values like:

  1. var lst = oTitle[event.value];
  2.  
  3. if (typeof lst !== "undefined") {
  4. getField(cRowName + ".isbn").value = lst[0];
  5. getField(cRowName + ".price").value = lst[1];
  6. } else {
  7. getField(cRowName + ".isbn").value = 0;
  8. getField(cRowName + ".price").value = 0;
  9. }
This assumes you have fields like "row1.price". How does cRowName get set?
pmarshall61
Registered: Sep 5 2011
Posts: 2
George_Johnson wrote:
It's not clear to me why that object is set up like that, but you can try something like the following:
  1. // Set up object to associate title with corresponding ISBN and price
  2. var oTitle = {
  3. "French book" : ["1234567890123", "None"],
  4. "German book" : ["9876543210123", "None"],
  5. "Song book" : ["1231234567890", "None"],
  6. "Italian book" : ["1239876543210", "None"]
  7. };
So each object property is a two element array, the first element is the ISBN number and the second is the price.

You can then access them and set the field values like:

  1. var lst = oTitle[event.value];
  2.  
  3. if (typeof lst !== "undefined") {
  4. getField(cRowName + ".isbn").value = lst[0];
  5. getField(cRowName + ".price").value = lst[1];
  6. } else {
  7. getField(cRowName + ".isbn").value = 0;
  8. getField(cRowName + ".price").value = 0;
  9. }
This assumes you have fields like "row1.price". How does cRowName get set?
Thanks George. Main problem was that I changed the var oTitle from a list to a single pair and did not remove the extra []. I have now got that part working as I wanted.

Pete