how get access to desired selector class from internal ajax success settings?
example: inside form with class 'formdataaj' are located one div with class 'img_ajx', where i want write text by ajax response.
My problem are that i can't access to selector by $(this). If i put fixed class $('.formdataaj .img_ajx').text(jsondata.image); work without problems, but obviously only in first class 'formdataaj'.
Thanks
my code:
$('.formdataaj').each(function(){
$(this).on('submit',function(e){
e.preventDefault();
var form_data = new FormData(this);
//console.log(...form_data);
$.ajax({
type: 'post',
url: 'php/extra_update.php',
data: form_data,
processData:false,
contentType:false,
cache: false,
success: function(data)
{
var jsondata = $.parseJSON(data);
console.log(jsondata);
$(this).find(".img_ajx").text(jsondata.image);
}
})
})
})
CodePudding user response:
// you need to asign [this keyword] to variable out of success scope
$('.formdataaj').each(function(){
var $this = $(this);
$(this).on('submit',function(e){
e.preventDefault();
var form_data = new FormData(this);
//console.log(...form_data);
$.ajax({
type: 'post',
url: 'php/extra_update.php',
data: form_data,
processData:false,
contentType:false,
cache: false,
success: function(data)
{
var jsondata = $.parseJSON(data);
console.log(jsondata);
$this.find(".img_ajx").text(jsondata.image);
}
})
})
})