Jquery loop with html -
i need loop content in <div class="newslist">
:
<figure class="effect-lily"> <img src="img/1.jpg" alt="img06"> <figcaption> <div> <h2><span>lily</span></h2> <p>lily likes play crayons , pencils</p> </div> <a href="https://tympanus.net/development/hovereffectideas/index.html#">view more</a> </figcaption> </figure>
my jquery is:
$(function() { $.ajax({ url: "test.json", datatype:'json', type: 'get', data:'data', success: function getdata(data){ console.log(data); var str = ''; var hasdata = 0; var newslist = $('.newslist'); for(var i=0; i<data.length;i++){ var ntitle = json.stringify(data[i].title); var desc = json.stringify(data[i].description); var writer = json.stringify(data[i].writer); var str = ''; console.log(data[i].title); console.log(data[i].description); if(data.length>0) { str += '<figure class="effect-lily"><img src="img/1.jpg" alt="img06"><figcaption><div><h2><span>'+ntitle+'</span></h2><p>'+desc+'</p></div><a href="https://tympanus.net/development/hovereffectideas/index.html#">view more</a></figcaption></figure>'; } } newslist.html(str); }, error: function(result){ console.log('error'); } }) });
but cannot loop content in <div class="newslist">
, it's show 1 item in spite of json has 7 item. what's wrong me?
you re-initialising variable str
within loop. declare var str = '';
outside of loop.
ie. move var str = '';
1 line above loop
Comments
Post a Comment