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]
}
});
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");
try if( $(response).filter('#result').length ) // do something
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 :)
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)
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);
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.
try this:
result = $("#result", response);
btw alert
is a rough way to debug things, try console.log
This worked for me, you just need to put .html() on the end of - $(response).find("#result")
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 :)
To view the content in alert use:
alert( $(response).find("#result").html() );
you just use the following code
var response= $(result);
$(response).find("#id/.class").html(); [or] $($(result)).find("#id/.class").html();
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
}
});
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: Use Jquery Selectors on $.AJAX loaded HTML?
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")
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.
$.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
Source: Stackoverflow.com