I’m making an ajax call to fetch content and append this content like this:
$(function(){
var site = $('input').val();
$.get('file.php', { site:site }, function(data){
mas = $(data).find('a');
mas.map(function(elem, index) {
divs = $(this).html();
$('#result').append('' + divs + '');
})
}, 'html');
});
The problem is that when I change a in body I get nothing (no error, just no html). Im assuming body is a tag just like ‘a’ is? What am I doing wrong?
So this works for me:
mas = $(data).find('a');
But this doesn’t:
mas = $(data).find('body');
Parsing the returned HTML through a jQuery object (i.e
$(data)) in order to get thebodytag is doomed to fail, I’m afraid.The reason is that the returned
datais astring(tryconsole.log(typeof(data))). Now, according to the jQuery documentation, when creating a jQuery object from a string containing complex HTML markup, tags such asbodyare likely to get stripped. This happens since in order to create the object, the HTML markup is actually inserted into the DOM which cannot allow such additional tags.Relevant quote from the documentation: