ListBox values

I apologize if this is a double post. I'm not sure if my previous question made it. So, let me try it again.

Using AcrobatX. What I'm trying to do is - I've created a ListBox and when selecting an item, it would be added to a separate txtField and would show a total. Select Item1 with the value of 10. Select Item2 with the value of 15. The txtField would add the values and return a total of 25 and would remain populated until reset or cleared. And so on. There's 10 item in the ListBox.

Can someone please help me figure out the javascript to get this done? I've been trying all day to find something on this site to help but could not. I saw an example of Thom Parker's ListBox. It's very close but doesn't have a field to show a total. So, can someone show me the javascript? Thanks.


jackson chaker


1 Answer

There are different ways to approach this. Here is one:

Create your listbox and your text box. To calculate the value in the text box, we need a custom calculation script , so open the preferences for the text box and select the Calculate tab and select a custom calculation script.

The following script will get all selected value and add them up:

var f = this.getField("ListBox1");
var a = f.currentValueIndices;
if (typeof a == "number") { // we have a single selection
    if (a == -1) { // nothing selected
        event.value = "";
    }
    else {
        event.value = Number(f.getItemAt(a, false));
    }
}
else { // multiple selections
    console.println("Found " + a.length + " selected values");
    var sum = 0;
    for (var i = 0; i < a.length; i ++) {
        sum += Number(f.getItemAt(a[i], false));
    }
    event.value = sum;
}

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   


Please specify a reason: