jQuery .find() on data from .ajax() call is returning "[object Object]" instead of div

JqueryAjaxFind

Jquery Problem Overview


Trying to find div element with id="result" in returned data from .ajax() using .find(). Unfortunately, alert(result) doesn't return div#result.

Here is my code:

$.ajax({
    url: url, 
    cache: false,
    success: function(response) {
        result = $(response).find("#result");
        alert(response); // works as expected (returns all html)
        alert(result); // returns [object Object]
    }
});

Jquery Solutions


Solution 1 - Jquery

To answer your question specifically, it seems to be working correctly. You said that it returns [object Object], which is what jQuery will return with the find("#result") method. It returns a jQuery element that matches the find query.

Try getting an attribute of that object, like result.attr("id") - it should return result.


In general, this answer depends on whether or not #result is the top level element.

If #result is the top level element,

<!-- #result as top level element -->
<div id="result">
  <span>Text</span>
</div>
<div id="other-top-level-element"></div>

find() will not work. Instead, use filter():

var $result = $(response).filter('#result');

If #result is not the top level element,

<!-- #result not as top level element -->
<div>
  <div id="result">
    <span>Text</span>
  </div>
</div>

find() will work:

var $result = $(response).find('#result');

Solution 2 - Jquery

I just spent 3 hours to solve a similar problem. This is what worked for me.

The element that I was attempting to retrieve from my $.get response was a first child element of the body tag. For some reason when I wrapped a div around this element, it became retrievable through $(response).find('#nameofelement').

No idea why but yeah, retrievable element cannot be first child of body... that might be helpful to someone :)

Solution 3 - Jquery

try this:

result = $("#result", response);

btw alert is a rough way to debug things, try console.log

Solution 4 - Jquery

this is your answer:

<div class="test">Hello</div>
<div class="one">World</div>    

The following jQuery Won't work:

$(data).find('div.test');    

as the divs are top level elements and data isn't an element but a string, to make it work you need to use .filter

$(data).filter('div.test');    

Another same question: https://stackoverflow.com/questions/405409/use-jquery-selectors-on-ajax-loaded-html

Solution 5 - Jquery

do not forget to do it with parse html. like:

$.ajax({
    url: url, 
    cache: false,
    success: function(response) {
        var parsed = $.parseHTML(response);
        result = $(parsed).find("#result");
    }
});

has to work :)

Solution 6 - Jquery

This worked for me, you just need to put .html() on the end of - $(response).find("#result")

Solution 7 - Jquery

The jQuery find() is returning a jQuery object that wraps the DOM object. You should be able to work with that object to do what you'd like with the div.

Solution 8 - Jquery

The thing is that your ajax response is returning a string, so if you use directly $(response) it would return JQUERY: Uncaught Error: Syntax error, unrecognized expression in the console. In order to use it properly you need to use first a JQUERY built-in function called $.parseHTML(response). As what the function name implies you need to parse the string first as an html object. Just like this in your case:

$.ajax({
    url: url, 
    cache: false,
    success: function(response) {
        var parsedResponse = $.parseHTML(response);
        var result = $(parsedResponse).find("#result");

        alert(response); // returns as string in console
        alert(parsedResponse); // returns as object HTML in console
        alert(result); // returns as object that has an id named result 
    }
});

Solution 9 - Jquery

Specify dataType: "html".

If you do not jQuery will guess the requested data type (check: http://api.jquery.com/jQuery.ajax/). My guess is that in your case response was a String rather than a DOMObject. Obviously DOM methods won't work on a String.

You could test that with console.log("type of response: " + typeof response) (or alert("type of response:" + typeof response), in case you don't run Firebug)

Solution 10 - Jquery

You can do it in this way to find any div and get its attributes or anything you want.

$(response).filter('#elementtobefindinresponse').attr("id");

or

$(response).filter('img#test').attr("src");

Solution 11 - Jquery

Is #result in the response HTML? Try the following. jQuery will still return an empty object if it doesn't find anything.

alert(result.length);

Solution 12 - Jquery

You should add dataType: "html" to the request. Im quite sure you wont be able to search the DOM of the returned html if it doesnt know it is html.

Solution 13 - Jquery

you just use the following code

var response= $(result);

$(response).find("#id/.class").html(); [or] $($(result)).find("#id/.class").html();

Solution 14 - Jquery

$.ajax({
    url: url,
    cache: false,
    success: function(response) {
        $('.element').html(response);
    }
});

< span class = "element" >
    //response
    < div id = "result" >
    	Not found 
    </div> 
</span>

var result = $("#result:contains('Not found')").text();
console.log(result); // output: Not found

Solution 15 - Jquery

try if( $(response).filter('#result').length ) // do something

Solution 16 - Jquery

To view the content in alert use:

alert( $(response).find("#result").html() );

Solution 17 - Jquery

You might have to do something like

var content= (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d

then you should be able to use

result = $(content).find("#result")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMenschView Question on Stackoverflow
Solution 1 - JqueryStephen WatkinsView Answer on Stackoverflow
Solution 2 - JqueryLiveSourceView Answer on Stackoverflow
Solution 3 - JqueryantpawView Answer on Stackoverflow
Solution 4 - JqueryBảo NamView Answer on Stackoverflow
Solution 5 - JqueryfunnyView Answer on Stackoverflow
Solution 6 - JquerySteve MatherView Answer on Stackoverflow
Solution 7 - JqueryRyanView Answer on Stackoverflow
Solution 8 - JquerysturzerblitzView Answer on Stackoverflow
Solution 9 - JqueryFK82View Answer on Stackoverflow
Solution 10 - JqueryHeartView Answer on Stackoverflow
Solution 11 - JqueryJason McCrearyView Answer on Stackoverflow
Solution 12 - JqueryFabianView Answer on Stackoverflow
Solution 13 - JqueryKarthikeyan GanesanView Answer on Stackoverflow
Solution 14 - JqueryKuldeep GuptaView Answer on Stackoverflow
Solution 15 - JqueryZhelyo HristovView Answer on Stackoverflow
Solution 16 - JqueryVikramView Answer on Stackoverflow
Solution 17 - JquerydrusnovView Answer on Stackoverflow