Copy link to clipboard
Copied
Hello!
How do I update a field with the newest selection in a combo box? I am relating to combo boxes by a selected index but my below code will only pull the previous selection and not the new selection. (I do have "Commit selected value immediately" checked)
Example: If index 3 is currently selected in combobox1 and I change my selection to index 100, the currentValueIndices outputs 3 instead of 100.
How do I update my field so that currentValueIndices returns 100?
if( event.willCommit ) {
if(event.value == "") this.resetForm(["combobox2"]);
else {
var field = this.getField("combobox1");
console.println(field );
var selectedIndex = field.currentValueIndices;
}}
Copy link to clipboard
Copied
In what context are you running your script (validation, format, calculation), and to which field is it attached to? Which combobox will trigger the change, and which combobox should be changed? Based on what I see in your script, it looks like the contents of combobox1 are the trigger. Keep in mind that currentValueIndices can be either a number, or an array if you have multiple values selected.
Copy link to clipboard
Copied
Thanks for the reply!
I am running my script in Custom Keystroke of combobox1. A selection in combobox1 will change the value of combobox2 through my function DescIndexLookup. Inputs for DescIndexLookup are the index of the value selected in combobox1 and the combobox that will be changed (ie combobox 2). The function performs as intended.
I'm only selecting one value in combobox1 so no need an array. My main issue is that currentValueIndices is passing along the changed from index instead of the changed to index (if that makes sense).
Below is another method I tried to pull the correct index but that didn't work either.
if( event.willCommit ) {
if(event.value == "") this.resetForm(["combobox2"]);
else {
var selectedIndex = event.target.currentValueIndices;
DescIndexLookup(selectedIndex , "combobox2");
}}Thanks!
Copy link to clipboard
Copied
Here is the problem with your code: It gets executed before the value is actually commited to the field. You have a chance to look at the value and change it (if you have a good reason to do that), so the keystroke or validation scripts are executed before the actual field value is updated. You are however trying to get the currently selected index from the field, and that is still set to what it was before you clicked on the new item. That is why you are seeing the previous value.
The newly selected value is in event.value (which you are testing to see if it's not emtpy), but this does contain the acutal selected value, not the index of the item in the dropdown control. This means that by using event.value, you should be able to do what you need to do, but you will have to change the logic that you use to determine what should be selected in your second dropdown control.
As a side note: I usually don't bother with the keystroke event for things like this: You can achieve the same with a custom validation script, and you don't have to deal with willCommit. In that case, the newly selected value would also be in event.value
Copy link to clipboard
Copied
Thank you.
The event.value method works about 60% of the time. I have changed my DescIndexLookup logic by including a dictionary in the function. The Keys are the selection in combobox1 and the Values are the corresponding output. This method works for a majority of the keys but I get an error for some of my keys (see below).
InvalidSetError: Set not possible, invalid or unknown.
Field.value:5:Field combobox1:Keystroke
Debugging attempts made:
-Copy my code into my IDE, it works perfectly.
-using console.println for combobox1 to see what event.value is being passed, all good there.
-All information is passed as a string and there is no type mismatch.
Are there any known issues with dictionaries in adobe?
Thanks again for all the help
Copy link to clipboard
Copied
No known issues. As long as it's valid JavaScript, it should run without a problem. I suspect that you are dealing with a type issue, and even though it looks OK when you print the values, the function that tries to set the value has a problem with the actual value that gets passed in. It's also possible that you have some extra characters before or after the value. The following assumed two simple dropdown controls, one with the values "one", "two", "three" and "four", and the second one with "1", "2", "3" and "4". I've added some debug output that shows the selected value, delimited by "|" so that you can easily see if there is some extra information, and I am printing the type of the data that I am receiving.
var lookup = {
"one" : 1,
"two" : 2,
"three" : 3,
"four" : 4,
};
if( event.willCommit ) {
if (event.value == "")
this.resetForm(["combobox2"]);
else {
console.println("|" + event.value + "|");
console.println(typeof(event.value));
var selectedValue = lookup[event.value];
console.println(selectedValue);
this.getField("combobox2").setItems( [ selectedValue ] );
}}
Copy link to clipboard
Copied
It seems like you are missing the point that Karl made, i.e., event.value does not contain the valid field value, because the field is in transition during the WillCommit event. And by extension the "selectedIndices" property is also incorrect. "event.value" can only be correct in the same way a broken clock is correct, things just happen to line up. Karl had a very good suggestion. Move the code to the Validate event and get rid of the first "if", the one that checks event.WillCommit.
Next the error you recieved is thrown when the value of a dropdown is set to something that is not already in the list.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more