More often programmers need to work with dropdownlist or combobox using
jquery. We need to manipulate the dropdownlist options using jquery
scripting. And it is the case that almost always we don't remember how
to implement jquery selector. And for all of my readers, who are
enthusiastic programmers and/or designers, and for myself, I am
discussing in this post the actions with jquery to manipulate asp.net
dropdownlist options.
Now append another option using jquery.
[Why am I using the selector syntax $('[id$=ddlTest]') instead of
$('#ddlTest')?
We use #ControlID to select a control with specified id. $= means
'select a control whose id ends with specified value. If you are using
master page, the rendered id of the dropdownlist is preceded with some
text.]
Now get the selected value from the dropdownlist.
Now get the selected texct from the dropdownlist.
Now, select a value programmatically.
And I hope you are comfortable with detecting through jquery when the use selects an item.
Good luck
- <asp:dropdownlist runat="server" id="ddlTest">
- <asp:listitem text="Nepalese Handicraft Products" value="Nepal">
- <asp:listitem text="Indian Handicraft Products" value="India">
- </asp:listitem></asp:listitem></asp:dropdownlist>
- $('[id$=ddlTest]').append('');
- var selVal=$('[id$=ddlTest]').val();
- var selTxt=$('[id$=ddlTest] option:selected').text();
- var selTxt=$('[id$=ddlTest]').val('Nepal');