Alex
Alex

Reputation: 9720

jQuery set DropDownList enabled

I have two DropDownList

<asp:DropDownList ID="ddl1" runat="server" Width="173px" 
                                CssClass="ddl" >
                            </asp:DropDownList>

<asp:DropDownList ID="ddl2" runat="server" Width="173px" 
                                CssClass="ddl" Enabled="False">
                            </asp:DropDownList>

I want when the first one will be ddl1.selectedindex = 3 to enable the second one ddl2

Upvotes: 1

Views: 2741

Answers (3)

Vimalnath
Vimalnath

Reputation: 6463

Try this:

if($('[id$="ddl1"]').val() == 3) {
   $('#ddl2').attr("disabled") = "disabled";
}

Upvotes: 2

HeavenCore
HeavenCore

Reputation: 7683

Just to provide the non-jquery inline approach:

<asp:DropDownList ID="ddl1" runat="server" Width="173px" 
     CssClass="ddl" onchange="if (this.selectedIndex=3) {document.getElementById('ddl2').disabled=false;}">
                            </asp:DropDownList>

<asp:DropDownList ID="ddl2" runat="server" Width="173px" 
                                CssClass="ddl" Enabled="False">
                            </asp:DropDownList>

Upvotes: 0

Willem
Willem

Reputation: 5404

You can use the .change event to check the selected value.

demo: http://jsfiddle.net/m8dX3/

javascript:

$('#ddl1').change(function(){
    var ddl1=$(this);
    var ddl2=$('#ddl2');
    if (ddl1.val()==3) { ddl2.removeAttr('disabled'); }
    else { ddl2.attr('disabled','disabled').val(0); }
});

and make sure you use the correct ClientID of the dropdownlists ($('#<%=ddl1.ClientID%>')) or use a classname.

Upvotes: 2

Related Questions