There are many class="item" blocks on the page.
For each one there is different var item_link and ajax request.
Ajax searches for src attribute of .message img and throws it to var src.
$(".item").each(function(){
var item_link = "http://...";
$(this).prepend('<div class="src"></div>');
$.get(item_link, function(data) {
var src = $('.message img', data).attr('src');
});
});
How to print var src to <div class="src"></div>?
Thanks.
jAndy’s approach should work, but it waits to add the div until the
GETcompletes (which should be fine). If it’s really important that you put the div in place before doing theGET, though, you could do this:That uses
item_div.text(), which will show the value fromsrc. Ifsrchas HTML tags and you want them to be rendered, useitem_div.html()instead.Edit: Update after your “doesn’t work” comment:
The part you asked about, setting the text of the div, works fine. I think the problem is that your code is assuming that the
datacoming back from theajaxcall will be a DOM element. It won’t, it’ll be a string until you insert it into the DOM somewhere (jQuery doesn’t turn HTML into DOM objects proactively).This version handles that:
Working example:
tempserver.jsp: