javascript - CSS formatting of AJAX json data -
i using jquery make call hardcoded json file. each json object has name, image, , description.
i need make page display each json listed item displayed in below mockup image.
page mockup
here script file:
$(function() { $.ajax( { url: 'javascripts/avocado.json', data: {} }).done(function(response) { console.log(response); (var = 0; < response.length; i++) { $('#avocado-list').append('<div class="avocado-item"></div>') $('.avocado-item').append('<h2>' + (i + 1) + '</h2>'); $('.avocado-item h2').addclass("avocado-number"); $('.avocado-item').append('<img src="../images/' + response[i].img + '">'); $('.avocado-item').append('<div><h3>' + response[i].name + '</h3>' + '<p>' + response[i].description + '</p></div>'); $('.avocado-item h3').addclass("avocado-title"); } }).fail(function() { console.log("error"); }).always(function() { console.log("complete"); }); });
and html trying load json data:
<section id="avocado-list" class="list"></section>
i trying create div of avocado-item each json object. trying append subsequent data (name, image, description) div can objects information in 1 line.
however creating 1 big div everything. doing wrong?
creating html way have done inefficient. more simple version of code should after.
$(function() { $.ajax({ url: 'javascripts/avocado.json', data: {} }).done(function(response) { console.log(response); var listmarkup = ''; (var = 0; < response.length; i++) { listmarkup += '<div class="avocado-item"><h2 class="avocado-number">' + (i + 1) + '</h2><img src="../images/' + response[i].img + '"><div><h3 class="avocado-title">' + response[i].name + '</h3>' + '<p>' + response[i].description + '</p></div></div>'; } $('#avocado-list').append(listmarkup); }).fail(function() { console.log("error"); }).always(function() { console.log("complete"); }); });
Comments
Post a Comment