1

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.

1

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

5 Comments

Why cant I use find() directly on parsed?
$.parseHTML() returns an array of dom nodes, not a jquery object.
@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.
How can I parse reply string into a jquery object then? I can not find required element nor by find() nor by filter() function
@Ask the updated answer should answer your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.