So you need to grab some pretty complex HTML - likely a whole page - via AJAX and extract a certain portion of it for use.
You're already using jQuery for your project, or it's not a big deal to add, so you figure it's super-simple and powerful selector functions like find
will make your life a whole lot easier.
I mean, you can just pass HTML into the top-level $()
function and it'll turn into a jQuery DOM representation so you can just chain a find
call onto that, right? Wrong.
Mostly due to the underlying browser APIs that jQuery has to rely on, complex HTML and especially entire pages get filtered into a big mess that makes using regex on the raw text response about as easy as using jQuery for it.
This is a compilation of basic examples of what you might try, and what works, based on my own futile experiments and this wonderful StackOverflow answer.
The first example is the one that works best and most reliably, as well as being the only one that works on whole pages, seeing as that's likely what you're most interested in.
Note: these examples won't work because example.com doesn't allow cross-origin requests. Test them with URLs you know will provide a proper response.