Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my second Ajax change selected value not work?

I have a JS function fire when my Dropdown for #Job changes. Three things are suppose to happen.

  • First, the BusinessAreaCode selects the appropriate item. This is
    working.
  • Second, I fetch the Division List to populate the
    DivisionCode Dropdown. This works as well.
  • Lastly, I set the appropriate DivisionCode just like I did for BusinessAreaCode.
    This is not working however.
$('#Job').change(function () {
            var selectedValue = this.value;

            if (selectedValue != null && selectedValue != '') {
                $.getJSON('@Url.Action("GetJobInfo")', { jobId: selectedValue }, function (info) {
                    console.log(info.BusinessAreaCode);
                    $('#BusinessAreaCode option[value="' + info.BusinessAreaCode + '"]').attr("selected", "selected");

                    var ddldivision = $('#DivisionCode');
                    ddldivision.empty();
                    ddldivision.append($('<option/>', {
                        value: "",
                        text: "-- Select --"
                    }));

                    var myValue = $('#BusinessAreaCode').val();
                    console.log(myValue)
                    if (myValue != null && myValue != '') {
                        $.getJSON('@Url.Action("BusinessAreaDivisions")', { businessArea: myValue }, function (divList) {
                            if (divList.length == 0) {
                                ddldivision.empty();
                                ddldivision.append($('<option/>', {
                                    value: "",
                                    text: "N/A"
                                }));
                            }
                            $.each(divList, function (XYZ, data) {
                                ddldivision.append($('<option/>', {
                                    value: data.Value,
                                    text: data.Text
                                }));
                            });
                        });
                    }
                    console.log(`'${info.DivisionCode}'`);
                    $('#DivisionCode option[value="' + info.DivisionCode + '"]').attr("selected", "selected");
                });
            }
        });

I don’t get an error in console log. I am sure I am returning a proper DivisionCode Value. Could this be a race condition? Any help will be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re trying to select the option before populating the list. The order of operations here is:

  1. Invoke getJSON to fetch "divisions"
  2. Set the selected "division"
  3. When getJSON completes, remove all "division" options and re-populate the list

So any selection made, if there were matching options at all, is clobbered as soon as the list is cleared and re-populated.

Move this line:

$('#DivisionCode option[value="' + info.DivisionCode + '"]').attr("selected", "selected");

So that it happens after the list is populated in the asynchronous operation:

$.getJSON('@Url.Action("BusinessAreaDivisions")', { businessArea: myValue }, function (divList) {
  // your code, then...
  $('#DivisionCode option[value="' + info.DivisionCode + '"]').attr("selected", "selected");
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading