Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,


I am executing a simple example of selecting the item from the dropdown list by using the value of a textbox.
The idea is:
If I have a dropdown list, suppose with values red, blue, green, yellow and so on.

And a simple html textbox.

Now suppose if I type red into the textbox then leave the textbox then automatically the dropdown list value should be selected to red value if it contains red value item.

I have tried many things in this but don't know the exact syntax.

thanks

Arvind Thakur
Posted

C#
function ChangeColors()
        {
            var colors = document.getElementById("colors");
            var entryColor = document.getElementById("entrybox").value;
            for (var i = 0; i < colors.length; i++)
            {
                if(entryColor == colors[i].innerText)
                {
                    colors[i].selected = true;
                }
            }
        }
 
Share this answer
 
Comments
123arvind123 29-Jul-11 8:17am    
thanks a lot...it really works
try to find Out in the following way

XML
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    <!--
    function setDropDownList(elementRef, valueToSetTo)
    {
     var isFound = false;
     for (var i=0; i<elementRef.options.length; i++)
     {
      if ( elementRef.options[i].value == valueToSetTo )
      {
       elementRef.options[i].selected = true;
       isFound = true;
      }
     }
     if ( isFound == false )
      elementRef.options[0].selected = true;
    }
    function setDatosCGA(strEstadoCarta)
    {
     setDropDownList(document.getElementById('<%=DropDownList1.ClientID %>'), strEstadoCarta);
    }
    function Button1_onclick() {
        setDatosCGA('cc');
    }
    // -->
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>aa</asp:ListItem>
            <asp:ListItem>bb</asp:ListItem>
            <asp:ListItem>cc</asp:ListItem>
            <asp:ListItem>dd</asp:ListItem>
            <asp:ListItem>ee</asp:ListItem>
        </asp:DropDownList>
        <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
    </div>
    </form>
</body>
 
Share this answer
 
Comments
123arvind123 29-Jul-11 7:41am    
sorry but I am using simple html dropdown and textbox..see my questions tags.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900