JavaScript - ...Dropdown list in jqery. - Asked By shah zeb on 25-Nov-11 07:15 AM

I need an example in jquery in which,
there should be a dropdownlist list loaded from through jquery.
On selecting a value from the dropdownlist. It should add the value to a textbox.
When reselecting value it should populate values other than the selected values and so on...
Any or a short demo is required.

dipa ahuja replied to shah zeb on 25-Nov-11 07:17 AM
Try this to get the selectedValue

$("#yourdropdownid option:selected").text();
Jitendra Faye replied to shah zeb on 25-Nov-11 07:20 AM

Try this:

$("#myselect :selected").text();

For an ASP.NET dropdown you can use the following selector:

$("[id*='MyDropDownId'] :selected")
shah zeb replied to dipa ahuja on 25-Nov-11 07:41 AM
It works But my main question was,
When i reselect values from the dropdownlist it should select only those values that are not already selected.
drop downlist shows A , B  and C.
if i select B and then reload it should only show  A and C.
This is the main question.

Suchit shah replied to shah zeb on 27-Nov-11 02:54 PM
$().ready(function() {
    type: "POST",
    url: "Default.aspx/GetGenders",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      $("#ddlGender").get(0).options.length = 0;
      $("#ddlGender").get(0).options[0] = new Option("Select gender", "-1");  
      $.each(msg.d, function(index, item) {
        $("#ddlGender").get(0).options[$("#ddlGender").get(0).options.length] = new Option(item.Display, item.Value);
    error: function() {
      alert("Failed to load genders");