May 14, 2012
jquery ajax cant loop thru the found data
Question by user759235
I want to loop thru a file which is loaded with ajax, but it won’t loop, i have tried a couple of things but I cant get it to work.
// jquery
$.ajax({
url: 'file.html',
type: "GET",
dataType: "html",
success: function(data) {
$(data).find('div').each(function(i){
alert('found')
});
},
error: function(){
alert('oeps...')
}
});
// file.html
<div>
// content goes here
</div>
<div>
// content goes here
</div>
<div>
// content goes here
</div>
...
...
Answer by Rocket Hazmat
You need to change .find
to .filter
. This is because .find
searches the children descendants of all the elements, but since your html file is just <div>
s, you need to use .filter
to find them.
Answer by Starx
You dont need to specify html
as the datatype, it is not needed.
SO, remove the following line.
dataType: "html"