I've got a complete HTML document I'm pulling in with $.ajax(), and my .done() callback looks like this:
function (data, text_status, jq_xhr) {
var $what_i_want = $(data).find('#what-i-want');
}
where data is a string that contains the entirety of a well-formed HTML document. This code never reaches .find().
Upon $(data), I get:
`Uncaught Error: Syntax error, unrecognized expression: <!DOCTYPE html>`...
The facts:
- I'm using jQuery 1.9.0
- The document is well-formed HTML5 according to the W3C validator.
I've used jQuery() to objectify many an HTML string, so I'm surprised this isn't working. Admittedly, I don't recall ever trying a whole document. Given the error, I'm guessing, perhaps, I need to escape this string somehow. But I'm not sure how.
Incidentally, this works:
var $what_i_want = $('#what-i-want', $.parseHTML(data))
But I can't figure out why the first approach fails.
$(snippet_string)and$.parseHTML(document_string). I wonder what the "official" word is on when to use$()and when to use$.parseHTML().