Skip to main content
July 14, 2016
Question

How can I get a list of selections in a select box? (Script on Page 387 of Acrobat JavaScript Scripting Refence not Working)

  • July 14, 2016
  • 2 replies
  • 663 views

I am trying to use the well known code on page 387 of Acrobat JavaScript Scripting Reference:

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

var a = f.currentValueIndices;

if (typeof a == "number") // a single selection

console.println("Selection: " + f.getItemAt(a, false));

else {// multiple selections

console.println("Selection:");

for (var i = 0; i < a.length; i ++)

console.println(" " + f.getItemAt(a, false));

}

However, in my code, it is not working (please see: <ACRO_source>Room:Keystroke</ACRO_source> at the bottom of the listing).  Most of the time it just produces the text:

Selection: FIRST_ELEMENT

Sometimes it produces a short list, but never the correct list of the selected elements in the select box.

//<Document-Level>

//<ACRO_source>Main</ACRO_source>

//<ACRO_script>

/*********** belongs to: Document-Level:Main ***********/

var buildingRoom = {};

var oFile = this.getDataObjectContents("export.csv");

var cFile = util.stringFromStream(oFile, "utf-8");

var arr = cFile.replace(/\r\n/g, ",").split(",");

var building;

var room;

for(var i = 0; i < arr.length-1; i++)

{

   building = arr.replace(/\r?\n|\r|"/g,'');

   if(!(building in buildingRoom))

        buildingRoom[building]=[];

   room = arr[++i].replace(/\r?\n|\r|"/g,'');

   buildingRoom[building].push([room,room]);

}

var buildingArr = Object.keys(buildingRoom);

var buildingArrItems = [];

for(var i = 0; i < buildingArr.length; i++)

    buildingArrItems.push([buildingArr,buildingArr]);

this.getField("Room").setItems(buildingRoom[Object.keys(buildingRoom)[0]]);

this.getField("Building").setItems(buildingArrItems);

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

//</ACRO_script>

//</Document-Level>

//<AcroForm>

//<ACRO_source>Building:Keystroke</ACRO_source>

//<ACRO_script>

/*********** belongs to: AcroForm:Building:Keystroke ***********/

if(event.willCommit)

{

//console.println("buildingEvent");

//console.println(event.value);

//console.println(buildingRoom[event.value]);

this.getField("Room").clearItems();

this.getField("Room").setItems(buildingRoom[event.value]);

}

//</ACRO_script>

//</AcroForm>

//<AcroForm>

//<ACRO_source>Room:Keystroke</ACRO_source>

//<ACRO_script>

/*********** belongs to: AcroForm:Room:Keystroke ***********/

var a = f.currentValueIndices;

if (typeof a == "number") // a single selection

console.println("Selection: " + f.getItemAt(a, false));

else {// multiple selections

console.println("Selection:");

for (var i = 0; i < a.length; i ++)

console.println(" " + f.getItemAt(a, false));

}

//</ACRO_script>

//</AcroForm>

This topic has been closed for replies.

2 replies

July 14, 2016

I placed a button on the page and configured it to execute the code on mouse up and it works as expected.  It appears that putting the code in the keystroke event itself isn't how it was supposed to be used.  I should have figured this out yesterday evening, as I was observing the phenomenon where I see different results under seemingly identical circumstances, which indicates it is a timing issue.

Bernd Alheit
Community Expert
Community Expert
July 14, 2016

During the keystroke event you get the old selection with your code, not the actual.

try67
Community Expert
Community Expert
July 14, 2016

I don't see where you defined the value of the "f" parameter...

Bernd Alheit
Community Expert
Community Expert
July 14, 2016

It is defined at document level.