I just been frustrated with it for couple of hours, need some help from real expert.
In below code, I expect clicking on the button with class fb_detail_btn
to be appended with the data result returned from .ajax function. However, what happened is that the previously non ajax appended data ($(this).append("<div>aaaaaa</div>");
) will always be erased once the ajax run. My test shows the php file has run correctly so the problem is why ajax returned data is not able to be appened to $(this) which assigned as a variable.
JavaScript
//GET FEEDBACK DETAIL METHOD//
function get_feeback_detail(result) {
alert('inside get_feeback_detail, btn_clicked_id is '+btn_clicked_id);
return $.ajax ({
type: "POST",
url: "../x_get/get_feedback_detail.php",
data:{fb_id: btn_clicked_id},
}).then(function(data) {
alert('then ran!');
//loading_effect_hide();
var dataSplit = data.split("|");
commCode = dataSplit[0];
//alert(dataSplit[1]);
if(commCode !=1){
$('#testInfo').html(dataSplit[1]);
$('#testInfo').addClass("errorRed");
//$('.fb_review_table_section').empty();
alert('returned false for then in ajax!');
return false;
}else{
result =dataSplit[1];
}
alert('result inside then is... '+result);
//alert(selectedDiv.attr('class'));
//return result;
selectedDiv.append(result);
//loading_effect_hide();
}).fail(function(x,s,e) {
alert(s+": "+e);
alert('detail button failed!');// an error occurred
//alert(result);
});//$.ajax END
alert('inside get_feeback_detail, result is... '+result);
};//get_feeback_detail() END
//Detail Button
$(document).on('click','.fb_detail_btn',function(){
selectedDiv=$(this);
selectedDiv.append("<div>aaaaaa</div>");
elementPropertyName = selectedDiv.attr('name');
btnNames = $(this).attr("name").split("_");
btn_clicked_id=btnNames[1];
result="";
get_feeback_detail();
selectedDiv.append("<div>bbbbbbb</div>");
});
HTML
<div class="fb_detail_btn" name="detail_68">aaadadadadadad</div>
=Update= Currently, the result of the work will reach .then but result will not be appended.