I am trying to show the json values in the dropdown list shown inside the table. My JSON value can be as shown below:
var ids1 = [{"pid":"23221","des":"2321"},{"pid":"1301","des":"1301"},{"pid":"1003","des":"1003"}]
var ids = [{"pid":"laptop","des":"laptop"},{"pid":"IPAD mini.","des":"IPAD mini."}]
It can be numbers as shown in the ids1 or can be string values as shown in ids or may be can get a combination.
When i use the below code, the values in the dropdown list are shown in the quotation marks.
$("<option />").text(JSON.stringify(value.des)).val(JSON.stringify(value.pid)).appendTo(productDropdown);
When used the below code to shown the values in the dropdown list, quotations are not shown for integer values shown in ids1, but when trying to pass ids and show in the list it is throwing the exception.
$("<option />").text(JSON.parse(value.des)).val(JSON.parse(value.pid)).appendTo(productDropdown);
Exception when trying to pass the ids in the loop.
Uncaught SyntaxError: Unexpected token l in JSON at position 0
Demo link : http://plnkr.co/edit/ilvGQPiy3nEKBuH8q46b?p=preview
js code:
function populateSelect(ids) {
var ids1 = [{"pid":"23221","des":"2321"},{"pid":"1301","des":"1301"},{"pid":"1003","des":"1003"}]
var ids = [{"pid":"laptop","des":"laptop"},{"pid":"IPAD mini.","des":"IPAD mini."}]
var productDropdown = $(".product");
$.each(ids, function(index,value) {
$("<option />").text(JSON.parse(value.des)).val(JSON.parse(value.pid)).appendTo(productDropdown);
// $("<option />").text(JSON.stringify(value.des)).val(JSON.stringify(value.pid)).appendTo(productDropdown);
});
}
$(document).ready(function(){
populateSelect();
});