javascript - Jquery find+length/size returns 0 -
i have divs created dynamically way:
//here goes loop, , works fine $("#result_main_search").append('<div class="singleresult_main_search"> <a href="http://somesite.com/" class="linktosight">' + sightslist[i]+ '</a> – ' + '<img src="/images/balloon.gif" rel="'+ +'" class="balloon_img_main_search" /></div>');
after loop, try set href attribute each link:
$('.singleresult_main_search').each(function() { $.get("_ajax_get_sight_link.php", {'id':$("img", this).attr('rel')}, function(data) { alert($(this).find('.linktosight').length); $(this).find('a').attr('href', data); alert(data); }); })
_ajax_get_sight_data.php
accepts id, returns link ( alert(data) works fine) . alert tells how .linktosight elements in current div gives 0 (by saying mean everytime finds 1 of generated divs). i've tried .size()
, $(this).find('a')
same result. so, how set work?
this
inside callback point jqxhr-object , not looped elements.
you may create closure:
$('.singleresult_main_search').each(function() { var $this=$(this); //..... });
..and use inside callback:
function(data) { alert($this.find('.linktosight').length); });
$.proxy() may option suggested jack franklin
Comments
Post a Comment