Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
215
Setting selected item/index in client-side code
posted

I am trying to learn how to programmatically set the value in a WebDropDown object (on the client).  Here is my test code:

function SetTo(value) {
  var combo = $find('<%= wddRoles.ClientID %>');
  var items = combo.get_items();
  var index = -1 
  console.log(items);   // returning NULL
  for (var i = 0; i < items.length; i++) {
      var item = items[i];
      if (item.get_element().get_value() == value) {
          index = i;
      }
  }
  console.log(index);
  combo.set_selectedItemIndex(index);

}     

As shown in the comments, the statement combo.get_items(); is returning null. I have checked the combo variable, and it is being set (i.e. it is not null).

Any thoughts on how to set the selectedIndex or selectedItem in a WebDropDown?

Parents
No Data
Reply
  • 215
    posted

    Again, not sure why .get_items() is returning null, but I did find a few other problems with my code:

    1. items.length does not work, it should be items.getLength()
    2. items[i] does not return a DropDownItem.  Need to use items.getItem(i)
    3. Once you have an item (DropDownItem), use item.get_value(), item.get_text() and item.get_index()
    4. item.get_element() returns DOM element - not needed here
    5. Use item.select() to select the item and then update the control's value (including input area) with combo.set_currentValue(item.get_text(), true);  You can also use combo.set_selectedItemIndex(item.get_index()) so that combo.get_selectedItemIndex() works

     

    ROB

Children