Creating a UL populated with JSON keys and values using jQuery
I have it.
HTML
<div id="catcher"></div>
jQuery
jQuery.ajax({
url:'./ajax.html',
async: true,
success: function(response){
for (var key in response){
jQuery('#catcher')
.append("<ul>"+ key +"<li>" + response[key].toString() + "</li></ul>");
}
}
})
JSON looks like
{
"Item":"Item Name",
"Item 1":"Item Name",
"Item 2":"Item Name",
"Item 3":"Item Name",
"Item 4":"Item Name"
}
I would like to do UL as follows
- Paragraph
- Element name
- Paragraph 1
- Element name
Etc. What I get now is each individual letter of the object value and number for the key. Instead, I want them both to be equal to the lines.
You should try adding console.log(response)to your callback. This will probably show that responseis a string, not an object.
dataType:'json' jQuery.ajax(...), : json- jquery/javascript?
. looped append <ul>, , :
var data = {
"Item": "Item Name",
"Item 1": "Item Name",
"Item 2": "Item Name",
"Item 3": "Item Name",
"Item 4": "Item Name"
}
$('#catcher').append('<ol></ol>');
for (var key in data) {
$('#catcher ol').append('<li>' + key + '<ul><li>' + data[key] + '</li></ul></li>');
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="catcher"></div>