I am passing HashMap
using jQuery
and JSON
to JSP
page. The values being returned to jQuery success
function are as follows:
Formatted JSON Data
{
"tableBird1":[
"001",
"Eurasian Collared-Dove"
],
"tableBird2":[
"002",
"Bald Eagle"
],
"tableBird3":[
"003",
"Cooper's Hawk"
]
}
I want to to populate a dropdown list
based on these values where a dropdown contains two values.
Here is the code I am trying:
$.ajax({
type:"GET",
url: "url?",
data: { groupName: $('#groupNamesDD').val() }, // Data being passed through json from other drop down list
dataType: "json",
success: function(response){
$.each(JSON.stringify(response), function(key,value) { // I get this exception here - Cannot use 'in' operator to search for '294' in
var element = document.createElement("option");
for(var i = 0; i < key.length; i++) {
element.value= JSON.stringify(key);
element.data-value = JSON.stringify(key.value); // here I get run time exception - invalid left hand assignment
element.text=JSON.stringify(key.value);
document.getElementById("displayColumnsDropDown").add(element);
}
});
}
});
I want the output something like this:
<select id="displayColumnsDropDown" >
<option value="">-- Select --</option>
<option value="tableBird1" data-value="001" >001</option>
<option value="tableBird1" data-value="Eurasian Collared-Dove" >Eurasian Collared-Dove</option>
<option value="tableBird2" data-value="002" >002</option>
<option value="tableBird2" data-value="Bald Eagle" >Bald Eagle</option>
<option value="tableBird3" data-value="003" >003</option>
<option value="tableBird3" data-value="Cooper's Hawk" >Cooper's Hawk</option>
</select>
I got the possible solution form this link Can an Option in a Select tag carry multiple values?
I don't know how to create more than one values for drop down list dynamically other than this way. Is there any other possible solution?
EDIT 1: Tried the following code:
$('#displayColumnsDropDown').append($('<option>', { value: key }).data-value(key.value).text(key.value));
In the second for
loop of success method. I still get the same exception:
Cannot use 'in' operator to search for '805' in