Dynamically Populate Options Of A Select Element From JSON

Dynamically Populate Options Of A Select Element From JSON
File Size: 4.16 KB
Views Total: 308
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

Have you ever wanted to bind JSON data to a dropdown list?

Here is a jQuery plugin that makes it possible to dynamically populate options of a native select element from a JSON file (or JSON API) you provide.

Ultra lightweight and easy to implement. You can easily modify the JS code to match your data set.

How to use it:

1. To get started, insert the JavaScript index.js after loading the latest jQuery JavaScript library.

<script src="/path/to/cdn/jquery.min.js"></script>
<script src="index.js"></script>

2. Initialize the plugin on your select element and specify the path to the JSON data.

<select id="myDropdown"></select>
$('#myDropdown').dynamicDropdown({
  url: 'json.json'
});
// data.json
[
  {"name":"jQuery", "url":"jQueryScript.net"},
  {"name":"JS & CSS", "url":"CSSScript.com"},
  {"name":"Google", "url":"Google.com"},
  {"name":"Youtube", "url":"Youtube.com"},
  {"name":"Facebook", "url":"Facebook.com"},
  {"name":"Instagram", "url":"Instagram.com"},
  // more data here
]

3. Determine the key & value names if necessary.

$('#myDropdown').dynamicDropdown({
  url: 'json.json',
  key: '',
  value: 'url'
});

This awesome jQuery plugin is developed by aromn. For more Advanced Usages, please check the demo page or visit the official website.