selectajax
A jQuery plugin to populate <option> element of html <select> input from ajax datasource.
Usage
<select id="selectCountry"></select>
<script src="jquery.js"></script> <script src="selectajax.js"></script> $('#selectCountry').getOptions({ url: "/api/country", key: "countryName", val: "alpha2Code", firstOption: { key: "Select Country", val: "" } });
url
returns -
[ { countryName: "Bangladesh", alpha2Code:"BD" }, { countryName: "Bhutan", alpha2Code:"BT" }, { countryName: "Malaysia", alpha2Code:"MY" } ... ]
Result:
<select id="selectCountry"> <option value="">Select Country</option> <option value="BD">Bangladesh</option> <option value="BT">Bhutan</option> <option value="MY">Malaysia</option> ... </select>