0

After performing XMLHttpRequest how can I parse responseText with jquery? I tried

var parsed = $.parseHTML(data);

but the result is DOM array and I can not select anything by $(parsed).find('#myIDobject') or so.

Praveen
  • 55,303
  • 33
  • 133
  • 164
TOP KEK
  • 2,593
  • 5
  • 36
  • 62
  • You can find your answer [here][1]. [1]: http://stackoverflow.com/questions/1811279/convert-string-into-jquery-object-and-select-inner-element – GDG Nov 25 '13 at 15:34

1 Answers1

1

And if not a collection of DOM elements, then what did you expect?

If the element you're trying to "find" is at root level, you'll need to use filter:

var parsed  = $.parseHTML(data); 

var element = $(parsed).filter('#myIDobject');

and to avoid the issue completely, you can do:

var parsed  = $.parseHTML(data); 

parsed = $('<div />').append(parsed);

parsed.find('#anything');
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Why cant I use find() directly on `parsed`? – TOP KEK Nov 25 '13 at 15:16
  • `$.parseHTML()` returns an array of dom nodes, not a jquery object. – Kevin B Nov 25 '13 at 15:19
  • @ask filter is required only if the element you are looking for is a top level element in the dom collection. `.find` only looks at descendants of the currently selected elements, and when you do `$(parsed)`, you're selecting all the top level elements contained within `parsed`. – Kevin B Nov 25 '13 at 15:20
  • How can I parse reply string into a jquery object then? I can not find required element nor by `find()` nor by `filter()` function – TOP KEK Nov 25 '13 at 15:24
  • @Ask the updated answer should answer your question. – Kevin B Nov 25 '13 at 15:55