« Designer Training at MAX | Main | Selecting Specific Database Records »

Getting a List's New Selection

Please note: This blog has been migrated to a new server. To ensure you get the most recent posts, updates and comments, please update your bookmarks to Stefan Cameron on Forms.

Have you ever struggled to figure-out what item from a list (list box or drop down list) a user had just selected in the list’s Change event? If so, it’s possible you were trying to use the

rawValue

property in order to get at this information.

Unlike other objects such as exclusion groups, the rawValue property of a list object doesn’t reflect the new selection until the selected value is committed to it (by the user tabbing or clicking away from the list). That means that if you’re trying to, say, make a certain field visible at the moment when a particular item in the list is selected, you can’t use the rawValue property because it still contains the old (previous selection) value.

Instead, you must use the

xfa.event.newText

object/property of the Change event itself and possibly the list object’s

boundItem

function in order to determine the value associated with the new selection.

When scripting any XFA event, you always have access to properties (information) of that event via the

xfa.event

object. In the case of the Change event (which occurs when the list’s selection changes), the

xfa.event.newText

property is of particular interest because it contains the text portion of the item that was just selected in the list. It’s important to note that this is only the text portion because if your list contains items with values that differ from their text (you’ve associated both a text and value part to each item in the list), you’re probably even more interested in determine the value associated with the new text that was just selected in the list. Fortunately, that’s an easy problem to solve as well:

this.boundItem( xfa.event.newText ); // JavaScript
$.boundItem( xfa.event.newText ) // FormCalc

will return the value bound (associated) to the text from the list’s new selection.

So there you have it: When handling a list object’s (list boxes or drop down lists) Change event, don’t rely on the rawValue to get the new selection: Use xfa.event.newText and boundItem(text) instead.

Comments

It seems to me that this doesn't work with multichoice listbox, isn't it?

Bye,
Alessio

OK, I solve the problem...
I put this script in the click event of a button and this button is executed when we exit from the listbox.
Remember that you have to put a default value for the listbox or the script won't work.

if ( xfa.form.form1.ListBox1.rawValue != null)
{
var lbValue = xfa.form.form1.ListBox1.rawValue.split("\n");
var nodeList = xfa.form.form1.ListBox1.resolveNode("#items").nodes;
var lbText = "";

for (var j = 0; j {
for (var i = 0; i {
if (xfa.form.form1.ListBox1.boundItem(nodeList.item(i).value) ==
lbValue[j])
{
lbText += nodeList.item(i).value + "\n";
break;
}
}
}

TextField1.rawValue = lbText;
}
Alessio

Alessio -- Thanks for that script!

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)