asp:DropDownList Select Item Client Side

ChrisMDrew
ChrisMDrew used Ask the Experts™
on
Simple one I'm sure but I can't find/work out the answer!

If I have an asp:DropDownList how do I find a specific item and select it client side.  For example I have a DropDownList containing 'fred' , 'Charlie', 'Bill', 'Harry'.  I want to display the DDL with 'Harry' Selected but I don't know the index of 'Harry' in the list.

Iassumed it would be something like

var index = ddlUsers.findItemByText('Harry');
ddlUsers.selectIndex = index
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006
Commented:
    function selectOptionByText(selectID, optionText) {
        var oSelect = document.getElementById(selectID);

        for (var i = 0; i < oSelect.options.length; i++) {
            if (oSelect.options[i].text == optionText) {
                oSelect.options[i].selected = true;
                break;
            }
        }
    }

    selectOptionByText('ddlUsers', 'Harry');

Open in new window

Author

Commented:
Fabulous - works perfectly!  I knew it would be simple - I guess I thought this functionality would be built in.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial