I have a map in my java sevlet and converting it to a json format that works right.
When I do this function below it creates a drop down, but it puts every character as an option?? This is what I got:
$(document).ready(function(){ var temp= '${temp}'; //alert(options); var $select = $('#down'); $select.find('option').remove(); $.each(temp, function(key, value) { $('<option>').val(key).text(value).appendTo($select); }); });
map content in JSON format
{"1" : "string","2" : "string"}
A pure Javascript solution: this snippet shows how to populate a dropdown select from JSON data (using id as value and name as text. The code creates a new Option object for each item in the JSON data and appends it to the select element with appendChild() . map is used in place of a for loop.
createElement('select'); langArray. forEach((element, index) => { let option_elem = document. createElement('option'); // Add index to option_elem option_elem. value = index; // Add element HTML option_elem.
I would do something like this:
$.each(temp, function(key, value) { $select.append(`<option value="${key}">${value}</option>`); });
JSON structure would be appreciated. At first you can experiment with find('element')
- it depends on JSON.
Only change the DOM once...
var listitems = ''; $.each(temp, function(key, value){ listitems += '<option value=' + key + '>' + value + '</option>'; }); $select.append(listitems);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With