user10561216
user10561216

Reputation:

Populate Select Option Boxes using j Query via ajax call

I have a drop-down(select field) which i am populating on the basis of another drop-down selected by the user

Ajax call to populate the first select option

  $.ajax({
              async: true,
              url : "OutletList",
                method : "GET",
                dataType : "json",
                contentType: "application/json; charset=utf-8",
                success: function( json ) {
                    $.each(json, function(i, value) {
                        $('#myselect').append($('<option>').text(value).attr('value', value));
                    });
                }
            });

the above code will populate the first select option

below one to populate new select option on the basis of 1st one

$('#myselect').on('change', function() {
        var selectedOutlet =this.value;
        alert(selectedOutlet);              
          $.ajax({
              async: true,
              url : "OutletCode",
                method : "GET",
                dataType : "json",
                contentType: "application/json; charset=utf-8",
                success: function( json ) {
                    $.each(json, function(i, value) {
                        $('#outletCode').append($('<option>').text(value).attr('value', value));
                    });
                }
            });

        });

This code is working fine but not populating the data as i wanted

Upvotes: 1

Views: 51

Answers (2)

gedhean
gedhean

Reputation: 112

You need to remove the preview options. You can put this code on success callback to remove all preview options:

$('#outletCode option').remove()

$('#myselect').on('change', function() {
        var selectedOutlet =this.value;
        alert(selectedOutlet);              
          $.ajax({
              async: true,
              url : "OutletCode",
                method : "GET",
                dataType : "json",
                contentType: "application/json; charset=utf-8",
                success: function( json ) {
                    # Remove preview options
                    $('#outletCode option').remove()
                    $.each(json, function(i, value) {
                        $('#outletCode').append($('<option>').text(value).attr('value', value));
                    });
                }
            });

        });

Upvotes: 1

Bogdan
Bogdan

Reputation: 399

It is because you use append on the second dropdown:

$('#outletCode').append($('').text(value).attr('value', value));

You have to remove the previously appended option to the second dropdown each time you append a new option. You can save the previous option in a global variable and remove it each time you append a new option to the second dropdown.

var previousOption;
$('#myselect').on('change', function() {
        var selectedOutlet =this.value;
        alert(selectedOutlet);              
          $.ajax({
              async: true,
              url : "OutletCode",
                method : "GET",
                dataType : "json",
                contentType: "application/json; charset=utf-8",
                success: function( json ) {
                    $.each(json, function(i, value) {
                        $('#outletCode').append($('<option>').text(value).attr('value', value));
                        $("#outletCode option[value="+previousOption]).remove();
                        previousOption = value;
                    });
                }
            });

        });

Upvotes: 0

Related Questions