Mathew Paul
Mathew Paul

Reputation: 663

dropdownlist selected index option not working with javascript

I am using an asp.net dropdownlist it contains 5 values the sample code is as follows

<asp:DropDownList ID="ddlSampledropdown" runat="server" Width="250px">
     <asp:ListItem Value="0" title="--Select--">--Select--</asp:ListItem>
     <asp:ListItem Value="1" title="Alpha">Alpha</asp:ListItem>
     <asp:ListItem Value="2" title="Bravo">Bravo</asp:ListItem>
     <asp:ListItem Value="3" title="Charlie">Charlie</asp:ListItem>
     <asp:ListItem Value="4" title="Delta">Delta</asp:ListItem>
     <asp:ListItem Value="5" title="Echo">Echo</asp:ListItem>
</asp:DropDownList>

I want to set the dropdownlist as disabled and set the selected text as alpha how can i do this with the help of a javascript in asp.net 2003

I made it as disabled but was unable to set the dropdownlist selected value

Upvotes: 0

Views: 3215

Answers (2)

Julius A
Julius A

Reputation: 39622

Using classic Javascript, you may consider doing something like this. Note: obtain the correct client Id for your drop down.

var mydropdown= document.getElementById("ctl00_ctl00_<clientIdForDropdown>");
mydropdown.options[1].selected="selected";

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44605

with JQuery, to select Alpha:

$("#ddlSampledropdown").val('1');

and to disable the control:

$("#ddlSampledropdown").attr("disabled", true);

sources:

Change the selected value of a drop-down list with jQuery

Disable drop down box on radio button click

Upvotes: 1

Related Questions