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]
}
});
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 thefind("#result")method. It returns a jQuery element that matches thefindquery.Try getting an attribute of that object, like
result.attr("id")– it should returnresult.In general, this answer depends on whether or not
#resultis the top level element.If
#resultis the top level element,find()will not work. Instead, usefilter():If
#resultis not the top level element,find()will work: