I need to replace the content of a div in my page with the html resultant from an ajax call.
The problem is that the html have some necessary scripts in it and it seems that jquery html() function stripts them out,
I need to filter the response and only get a specific div.
I am thinking on a workaround which is to extract all the script tags from the ajax response and then append them do the DOM but i am having trouble doing that.
Here is my code;
$('a.link-vote').live('click',function(){
var idfeedback = $(this).attr('id').split('-')[1];
var href = $(this).attr('href');
$('.feedback-' + idfeedback + '-loader').show();
$.ajax({
type: "POST",
url: href,
success: function(response){
var x = $(response).find('#feedback-'+ idfeedback).html();
$('.feedback-' + idfeedback + '-loader').hide();
$('#feedback-'+ idfeedback).html(x);
}
});
return false;
});
I found this old topic:
jQuery - script tags in the HTML are parsed out by jQuery and not executed
but the is any conclusion. I tried the solutions suggested there but none of them work.
EDIT:
I seem to found a workaround based on that old topic but it′s not pretty;
var dom = $(response);
// var x = $(response).find('#feedback-'+ idfeedback).html();
$('.feedback-' + idfeedback + '-loader').hide();
//$('#feedback-'+ idfeedback).html(x);
$('#feedback-'+ idfeedback).html(dom.find('#feedback-'+ idfeedback).html());
dom.filter('script').each(function(){
var obj = $(this);
$('#feedback-'+ idfeedback + ' .feedback-comments').append(obj);
});
There must be a easy way.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…