javascript - Select content within a variable instead of within the current DOM with jQuery? -



javascript - Select content within a variable instead of within the current DOM with jQuery? -

i have ajax function using jquery defines error function called. when error occurs on server error function runs. 1 of variables passed in "jqxhr" contains property called responsetext. want dump response text div on page, response text contains fully-formed html document. there way utilize jquery traverse variable containing html in same way traverse regular dom?

$.ajax({ blah blah blah..., error: function (jqxhr, textstatus, errorthrown) { var errortext = $(jqxhr.responsetext).find('body').html(); // above line not work. errortext null. $('#maincontent').html(errortext); } });

i above code snippet way i'm doing not work. there way traverse variable if dom navigate jquery?

update

here console.log($(jqxhr.responsetext))

some sweetness discovery me on one.

this doesn't work because behind scenes, jquery creating document fragment , setting innerhtml property on it. does not work <html> , <body> nodes because aren't document node types--they cannot placed dom.

instead, when phone call $('<html><body><b>foo</b></body></html>'), fragment created "<b>foo</b>" in it! so, want body part? return:

$(jqxhr.responsetext).html();

fiddle prove i'm referring to: http://jsfiddle.net/l5pr5/1/

edit #2

i think choice, given <head> elements beingness set in there, utilize substrings:

var res = jqxhr.responsetext; $(res.substring(res.indexof('<body'))).appendto('#maincontent');

javascript jquery html ajax dom

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -