I tried to find a tag in the result from a ajax function and use data() to set value 'file', file_number
like below, but I can't see it in console.log, undefined is what i get.
is there something wrong with my code?
....success: function(html){
var result = $(html).filter('.result').html();
// console.log(result);
var file_number = $(html).find('.file_number').html();
// console.log(file_number); // will get number
//
$(html).find('img').data('file', file_number);
console.log($(html).find('img').data('file'));
is there something wrong with my code?
Yes. You are calling $(html)
multiple times, i.e. jQuery will parse the HTML string multiple times and create different DOM elements from it (each time). The changes you make to one of those parsed sets are not persistent and don't have any influence on any of the others.
Store the result of $(html)
and only work on this one set of DOM elements:
var $html = $(html);
var result = $html.filter('.result').html();
// console.log(result);
var file_number = $html.find('.file_number').html();
$html.find('img').data('file', file_number);
console.log($html.find('img').data('file'));