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
1360
How to set WebDropDown selection via VALUE not TEXT
posted

Is there an efficient way (ClientSide) to select an item in a WebDropDown based on the VALUE (happens to be an Integer) rather than the TEXT? The method [set_currentValue] does not do what I expected (it should be called [set_CurrentText] IMHO.)

I currently have to scan the items in the dropdown list looking for the one that has the correct value and then use its text to set the selected item, thus:

  function setDROP(clientID, val) {  // clientID of the WebDropDown, val = INT value
     var dd = $find(clientID);                  // get the dropdown control
     dd.set_selectedItemIndex(-1);  // de-select all
     var items = dd.get_items();  // get the items[]
     var iCount = items.getLength();  // how many we got?
     for (var i = 0; i < iCount; i++)  // scan the items
     {
         var itm = items.getItem(i);  // get the next item
         if (itm.get_value() == val)  // does its value match?
         {
            dd.set_currentValue(itm.get_text(), true);  // Yes, set the WebDropDown by specifying the text associated with the value
            break;    // we are done...
         }
     }
  }

This is VERY inefficient and requires on average a scan of half of the items in the list, yuk.

Any ideas on how best to do this? Surely there must be a better way.

Kind regards,

Paul

Parents Reply Children