0
 function Test_Country_OnItemSelection(ddlCountryId, ddlcityId, DataList) {
    var ddlCountry = document.getElementById(ddlCountryId);
    var selectedValue = ddlCountry.options[ddlCountry.selectedIndex].value;
    var selectedText = ddlCountry.options[ddlCountry.selectedIndex].text;

    var ddlcity = document.getElementById(ddlcityId);
}

I have the above JavaScript function in which I'm passing parameters for dropdownlists ddlCountryId and ddlcityId. I then pass the final parameter DataList which is a C# object defined as List<City> and it contains a CityId, CityName & CountryID.

How can I convert the DataList object to a JavaScript array that I can then filter on CountryID == selectedValue and then refresh dropdownlist ddlcity after applying my filter?

3
  • Unrelated protip: You don't need to use ddlCountry.options[ddlCountry.selectedIndex].value - just use ddlCountry.value. Also, it is incorrect to refer to <select> elements (HTMLSelectElement) as a "drop-down": that only applies to single-mode <select> elements on desktop browsers, on mobile devices they're not drop-downs at all. Commented Feb 7, 2022 at 13:24
  • @Dai That's a distinction without a difference (render JSON in a script and it's JavaScript). The accepted answer uses ajax, but the questions are the same. There are answers on that question which do not use ajax and will render the array inline. Commented Feb 7, 2022 at 13:30
  • @Dai I don't care. The fact that you find an answer sub-par does not make the questions not duplicates. If you have a better answer, answer the duplicate. If you have a problem with an answer, comment on the answer. Don't say the questions are not duplicates just because you don't like the answers. Commented Feb 7, 2022 at 13:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.