 <pre><code>var outputHTML = ''; for (var i = 0; i &lt; data.media.length; i++) { if (data.media[i].image != undefined) { outputHTML += '&lt;div class="image"&gt;'; // for (var j = 0; j &lt; data.media[i].image.length; j++) { // console.log(data.images[i]); // Extract filename var url = data.media[i].image.src; var filename = url.substring( url.lastIndexOf('/') + 1, url.lastIndexOf('.') ); // console.log(filename); outputHTML += '&lt;img src="' + url + '" alt="' + filename + '" /&gt;'; // $("body").append(outputHTML); // }; outputHTML +='&lt;/div&gt;'; } }; return outputHTML; </code></pre>