Page 1 of 1

extract items from json encoded array with jquery

Posted: Fri Apr 25, 2014 1:05 pm
by AnoPem
Hello im trying to echo an array into div boxes however i cant get it working probaly, so heres my question. how can i do so that this code will create a div box and populate it with my array

heres my code i have so far,
Code: Select all
$.each( data, function( key, value ) {
	$.each( value, function( key2, value2 ) {
		$('<h1/ id="test">').text(value2).appendTo('#search_results');
	});
});
and this is my array, its inside another array so double array, and is json encoded
Code: Select all
[{"id":"4","title":"test1"},{"id":"5","title":"test2"},{"id":"6","title":"test3"},{"id":"7","title":"test4"},{"id":"9","title":"test6"}]
i would like it to look like this
Code: Select all
<div class="myclass">
   <h2>The title</h2>
   <h2>The id</h2>
</div>
<div class="myclass">
   <h2>The title</h2>
   <h2>The id</h2>
</div>

Re: extract items from json encoded array with jquery

Posted: Fri Apr 25, 2014 1:33 pm
by AnoPem
This has been resolved with this code
Code: Select all
$.each(data, function() {
$('#search_results').append('<div class="result"><a href="item.php?id=' + this.id + '">' + this.title + '</a>');
});