I have this json
"urls": {
"title1": {
"url": "someurl1"
},
"title12": {
"url": "someurl2"
}
}
I want get title and value of url out in an element for each. Something like this
$.each(value.urls, function (key, value) {
$('.log-list-js').append('<a href="'+ title +'">'+ someurl +'</a>');
});
Hope it makes sense.
Can you help me ?
asked Nov 18, 2013 at 12:56
Newcoma
8094 gold badges14 silver badges31 bronze badges
3 Answers 3
Try this
var values = {
"urls": {
"title1": {
"url": "someurl1"
},
"title12": {
"url": "someurl2"
}
}
}
for (var key in values.urls) {
$('.log-list-js').append('<a href="' + values.urls[key].url + '">' + values.urls[key].url + '</a>');
}
answered Nov 18, 2013 at 12:59
Anton
32.6k5 gold badges47 silver badges54 bronze badges
$('.log-list-js').append('<a href="' + value.urls[key].url + '">' + value.urls[key].url + '</a>');
This is format
answered Nov 18, 2013 at 13:01
Kaushik
2,0901 gold badge24 silver badges32 bronze badges
Comments
Your json should be converted to object first, use JSON.parse method if you are having a json string. object should something like this
urls = {
"title1": {
"url": "someurl1"
},
"title12": {
"url": "someurl2"
}
};
then your method is correct with some corrections to the code
$.each(urls, function(index, value){
$('.log-list-js').append('<a href="'+ index +'">'+ value.url +'</a>');
});
here is the demo on jsfiddle
answered Nov 18, 2013 at 13:37
Ruwanka De Silva
3,8256 gold badges39 silver badges55 bronze badges
Comments
lang-js