jQuery('ol.productList li.product input[name*="cart.pAsin."]').load().each(function(){ // each function
var asinId = jQuery(this).val();
var pUrl = jQuery(this).siblings("a").attr('href');
var imgPath = jQuery(this).siblings("a").children('img').attr('src');
var productTitle = jQuery(this).siblings("div.productDetails").children('h4.title').text();
var descUrl = '/api/product/asin/' + asinId;
var productDesc = '';
jQuery.getJSON(descUrl, function(data) {
var attr = data.attributes[1];
productDesc = attr.productDescription;
var allinfo = [];
allinfo.push(asinId,productTitle,productDesc,pUrl,imgPath);
alert(allinfo[0]);
});
});
alert('Asin id :'+ asinId + ' Product Url : '+ pUrl + ' Image Path : ' +imgPath + ' Product Titl : '+productTitle+ ' Product Discription : ' +productDesc );
jQuery(‘ol.productList li.product input[name*=cart.pAsin.]’).load().each(function(){ // each function var asinId = jQuery(this).val(); var pUrl = jQuery(this).siblings(a).attr(‘href’);
Share
You can’t access
asinId,pUrl, etc as they are defined within the scope of anonymous function you passed toeach()method.To even make matters more confusing, the
productDescmight not also be the value you are looking for, even if you try to access it within the said anonfunctionasgetJSONis operating underAJAXand is asynchronous, meaning the value ofproductDescafter.getJSONcall can’t be guaranteed to be assigned after.getJSONhandler finished.You could define asinId etc out of the
each()method and then it’ll be available outside the anon function scope: