locked
Disabling a value in dropdownlist- Javascript RRS feed

  • Question

  • User111556086 posted

     Hi

     

    I am trying to disable a value in my dropdownlist depending on what is written in my textbox. Does anyone have an idea

     

    thanks

    ck

    Friday, January 8, 2010 6:55 AM

Answers

  • User-1171043462 posted

    This way

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <script type = "text/javascript">
        function Check(value) {
            var items = document.getElementById("<%=DropDownList1.ClientID %>").options;
            for (var i = 0; i < items.length; i++) {
                items[i].disabled = (items[i].text.toLowerCase() == value.toLowerCase());
            }
        }
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Text = "One" Value = "1"></asp:ListItem>
            <asp:ListItem Text = "Two" Value = "2" ></asp:ListItem>
            <asp:ListItem Text = "Three" Value = "3"></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="TextBox1" runat="server" onkeyup = "Check(this.value)"></asp:TextBox>
        </form>
    </body>
    </html>


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2010 7:39 AM

All replies

  • User-1659704165 posted

    Hi,

    http://www.java2s.com/Code/JavaScript/Form-Control/Disableandenableadropdownlistcombobox.htm

    the above link Shows simple code snippet how to disable combox using javascript

    u use with textvalue condition

    Friday, January 8, 2010 7:24 AM
  • User-1360095595 posted

    There's no easy way to do it.  You can change the style of the item to make it look like it's disabled, and then with the help of javascript, you can "ignore" that selection (i.e. stop the postback).  That's one way, I think, that you can simulate disabling an item.

    Friday, January 8, 2010 7:32 AM
  • User111556086 posted

    Thanks for you answer i already checked that link which didn't help a lot.

     

    I already wrote little kod but that is not getting me so far.

     

    // CK

    Friday, January 8, 2010 7:38 AM
  • User-1171043462 posted

    This way

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <script type = "text/javascript">
        function Check(value) {
            var items = document.getElementById("<%=DropDownList1.ClientID %>").options;
            for (var i = 0; i < items.length; i++) {
                items[i].disabled = (items[i].text.toLowerCase() == value.toLowerCase());
            }
        }
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Text = "One" Value = "1"></asp:ListItem>
            <asp:ListItem Text = "Two" Value = "2" ></asp:ListItem>
            <asp:ListItem Text = "Three" Value = "3"></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="TextBox1" runat="server" onkeyup = "Check(this.value)"></asp:TextBox>
        </form>
    </body>
    </html>


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2010 7:39 AM
  • User111556086 posted

    Hi thanks for the hint but i think i'm missing something 

     

     

     


     

     

     

    Friday, January 8, 2010 8:07 AM
  • User-1171043462 posted

    onKeyup='MaxAmountDepositMethodFirst

     


    '

     

     

    Replace above with

    Onkeyup = "MaxAmountDepositMethodFirst()"

    and use the following function

     function MaxAmountDepositMethodFirst()
    {
        var theForm = document.forms['<%=Form.ClientID %>'];
    if (!theForm)
    theForm = document.<%=Form.ClientID %>;
    var depositFirst = theForm.<%=txtDepositFirst.ClientID %>;
    var maxDepositAmount= "5000000";
    //var depositMethodFirst = document.all.ddlDepositMethodFirst;
    var engangSelect= "Engangsfullmakt";

    if( depositFirst > maxDepositAmount)
    {
        var items = document.getElementById("<%=ddlDepositMethodFirst.ClientID %>").options;  
                       for (var i = 0; i < items.length; i++)
                       {
                        if (items[i]== engangSelect)
                        {
                         items[i].disabled = (items[i].text.toLowerCase() == value.toLowerCase());  
                        }
                 
       }

    }


    }

     

    Friday, January 8, 2010 8:37 AM
  • User111556086 posted

     

    thank you all for your help . I found what was wrong

    // Ck

    Friday, January 8, 2010 8:55 AM
  • User111556086 posted

     Hey guys

     im stuck with a really crazy problem. Disabling a value in dropdownlist with help of javascript works in IE8 but not in IE 7 and IE 6.  does anyone knows anything about that.

     

    thanks

    CK

    Tuesday, February 9, 2010 9:55 AM
  • User798903548 posted

     Hey guys

     im stuck with a really crazy problem. Disabling a value in dropdownlist with help of javascript works in IE8 but not in IE 7 and IE 6.  does anyone knows anything about that.

     

    thanks

    CK

     

    Start your own post with the relevent code.

    NC...

     

    Tuesday, February 9, 2010 10:08 AM