1.0.2 • Published 4 years ago
dynamicdropdown-aromn v1.0.2
dynamicdropdown-jquery
a simple jquery plugin to feed a dropdown with json data.
dependencies:
* jquery
* bootstrap(not necessary but make it looks better).
instructions
Clone repository:
$ git clone https://github.com/aromn/dynamicdropdown-jquery.git
Call the plugin in your HTML select dropdown:
<select class="form-control" id="myDropdown"></select> $('#myDropdown').dynamicDropdown({ url: ''http://your-url/' });
Optionally you can add form-control class from bootstrap, or the plugin will add it by itself.
Modify the getJSON function inside to play around with the data
// AJAX call to the url provided in the plugin's settings. $.getJSON(settings.url, function(result) { // If the returned data is a JSON object, it proceeds to build the dropdown // IMPORTANT: It relies on the structure of the JSON object returned. $.each(result, function(i, value) { // Appending data to the dropdown referenced by the variable this_obj this_obj.append('<option>' + result.something + ' - ' + result.somethingelse + '</option>'); }); // If the call to the URL don't return a json object, it returns an error. }).fail(function() { console.log('%c ERROR: ' + 'Response data from ' + settings.url + 'is not a JSON object', 'background: red'); })