Friday, August 22, 2014

jQuery .find() on data from .ajax()

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>
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'); 

No comments:

Nathuram Godse - The Man Who Killed Gandhi

Nathuram Godse - The Man Who Killed Gandhi (The Other Side of The Story)