I have a json object stored as a JavaScript string var containing both names and values for populating a dropdown box, say #dropdown. My question is, how can I access each element as value/label pairs and put them into a dropdown?
The json string looks like this:
[{"name":"Name1","value":"Val1"},{"name":"Name2","value":"Val2"}]
Does this need to be further parsed in any way?
I tried something like (but did not work)…
$(json).map(function () {
return $('<option>').val(this.value).text(this.name);
}).appendTo('#copy_input');
In what way do I need to further format my json to be able to easily iterate and fill the dropdown?
Can you try below and see if this works?