Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an ajax function using jQuery that defines an error function to be called. When an error occurs on the server this error function runs. One of the variables passed in "jqXHR" contains a property called responseText. I want to dump this response text into a div on the page, but the response text contains a fully-formed HTML document. Is there any way to use jQuery to traverse this variable containing HTML in the same way I would traverse the regular DOM?

$.ajax({
    blah blah blah...,
    error: function (jqXHR, textStatus, errorThrown)
    {
        var errorText = $(jqXHR.responseText).find('body').html();
        // The above line does not work. errorText is NULL.
        $('#mainContent').html(errorText);
    }
});

I would like to do something like the above code snippet but the way I'm doing it does not work. Is there a way to traverse this variable as if it were a DOM that I could navigate with jQuery?

UPDATE

Here is a console.log($(jqXHR.responseText))

http://www.codetunnel.com/content/images/consolelog.jpg

like image 569
Chev Avatar asked Jul 08 '11 22:07

Chev


People also ask

Can you use a variable in a jQuery selector?

Yes, it is possible to pass a variable into a jQuery attribute-contains selector. The [attribute*=value] selector is used to select each element with a specific attribute and a value containing a string.

How do I select elements when I already have a DOM element?

If you have a variable containing a DOM element, and want to select elements related to that DOM element, simply wrap it in a jQuery object. var myDomElement = document. getElementById( "foo" ); // A plain DOM element.

What is $() in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)

Which is the correct jQuery selector to select current HTML element?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.


2 Answers

Some sweet discovery for me as well on this one.

This doesn't work because behind the scenes, jQuery is creating a document fragment and setting the innerHtml property on it. This does not work with the <html> and <body> nodes because those aren't document node types--they cannot be placed into the DOM.

Instead, when you call $('<html><body><b>foo</b></body></html>'), a fragment is created with just "<b>foo</b>" in it! So, you want just the body part? Just return:

$(jqXHR.responseText).html();

Fiddle to prove what I'm referring to: http://jsfiddle.net/L5PR5/1/


EDIT #2

I think your only choice, given that <head> elements are being put in there, is to use substrings:

var res = jqXHR.responseText;
$(res.substring(res.indexOf('<body'))).appendTo('#mainContent');
like image 99
Adam Terlson Avatar answered Sep 24 '22 05:09

Adam Terlson


Try this:

var errorText = $('<div />').append(jqXHR.responseText).html();

and here's a jsfiddle.

like image 35
Darin Dimitrov Avatar answered Sep 26 '22 05:09

Darin Dimitrov