Tuesday, January 19, 2010

Add new value in .net dropdownlist control using jQuery

 

Here is your jQuery code:

// first lets un-select any items that have been selected 
$("select.ddlMyDropDown option:selected").removeAttr("selected");
var addvalue = ‘MyNewValue’;

$("select.ddlMyDropDown").prepend('<option selected="selected" value="' + addvalue + '">' + addvalue + '</option>');

Here is your html code:

<asp:DropDownList runat="server" ID="ddlMyDropDown" CssClass="ddlMyDropDown" ></asp:DropDownList> 
<br />
<input type="text" id="addToDropDown" class="addToDropDown" visible ="false" size="1" /> 

Nice!

No comments:

Post a Comment