Skip to main content
Legend
August 3, 2012
Answered

Setting and getting a drop down list selection

  • August 3, 2012
  • 1 reply
  • 7803 views

Hi,

This is a general ES question, but since I'm working with FM, I'll try here first.

I am able to create dropdown lists with ES and set/get the current selection, but I'm not clear on why it is working the way it does. It seems that when I set the selection, it uses an index of the current items, but when I get the selection, it gets the actual value.

Consider this sample, with its comments:

//create the dropdown, for the dlg dialog

dlg.dropDown = dlg.add("dropdownlist", undefined, [10, 20, 30]);

//this sets the current value in the box to 30

dlg.dropDown.selection = 2;

//however, this reports 30, not 2

alert(dlg.dropDown.selection);

Does anybody know about this? Is it true that it is an index when set, and a value when gotten? I couldn't find anything in the documentation about it.

Thanks,

Russ

This topic has been closed for replies.
Correct answer Russ Ward

Nevermind, I got it. The 'selection' property is actually a ListItem object, not just a primitive data type as I had thought. So, the default property of the ListItem is chosen differently in different scenarios. To clarify:

dlg.dropDown.selection.index

...is the index

dlg.dropDown.selection.text

...is the text value

So I should be using the full property to be sure.

Russ

1 reply

Russ WardAuthorCorrect answer
Legend
August 3, 2012

Nevermind, I got it. The 'selection' property is actually a ListItem object, not just a primitive data type as I had thought. So, the default property of the ListItem is chosen differently in different scenarios. To clarify:

dlg.dropDown.selection.index

...is the index

dlg.dropDown.selection.text

...is the text value

So I should be using the full property to be sure.

Russ

Participant
September 11, 2012

I'm having similar issues and this thread doesn't quite provide the answer.

In your original example:

//create the dropdown, for the dlg dialog

dlg.dropDown = dlg.add("dropdownlist", undefined, [10, 20, 30]);

//this sets the current value in the box to 30

dlg.dropDown.selection = 2;

//however, this reports 30, not 2

alert(dlg.dropDown.selection);

...

Say that you want to set the dropDown.selection, not with an index  number, but with the text.

Setting. dropDown.selection.text = "30" doesn't actually change the dropDown.selection. What it does is change the CURRENT dropDown.selection to say "30". (So, if you had "20" selected, it'd change it to say "30", but then your list would include "10, 30, 30"

Numerous pieces of my script need to select items in lists based on the string-name of the listItem. (The positions of the items change sometimes, so using the index doesn't work)

If my dropDown menu includes "a", "b", and "c", how do I get something like this to work:

>>>dropDown.selection = "a"?

Russ WardAuthor
Legend
September 12, 2012

Raemon_777,

I don't know a direct way to do this, but I can figure out a roundabout method. Here's what I know, which isn't all that much...

A dropdownlist object has an "items" property, which is an array of listitem objects that represent the selections. For example, in your list that contains "a", "b", and "c", you'll have three listitems in the array. Furthermore, each listitem has two important properties:

"text" which is the text value that displays

"index" which is the index in the "items" array, starting at 0 for the first one.

So, in the a,b,c example, you have these properties:

dropDown.items[0].text     (equals "a")

dropDown.items[0].index   (equals 0)

dropDown.items[1].text   (equals "b")

dropDown.items[1].index   (equals 1)

dropDown.items[2].text   (equals "c")

dropDown.items[2].index   (equals 2)

And, the "selection" property of the dropdown object is just a reference to one of these listitems object. So, if you want to make the list show "c", you could do:

dropDown.selection = dropDown.items[2]

...but, you say you don't know the index, just the text value. So, I worked up this little function which seems to do the trick. Send it the dropdownlist object and the text value you want displayed. It will find the correct listitem object and set it as the "selection" property, rather than your previous method that was simply resetting the text value of the current selection listitem.

Like I said, there might be a better way to do this. In a hurry, this is the best I could conjure up.

Russ

function SetDropDown(dropdown, value)

{

    for(i = 0; i < dropdown.items.length; i++)

    {

       if(dropdown.items.text == value)

       {

           dropdown.selection = dropdown.items;

           i = dropdown.items.length;

       }

    }

}