peteykun
peteykun

Reputation: 716

Get content of div from HTML string

var html = '<html><body><header>Header content</header><div id="container">Content I want</div><footer>Footer content</footer></body></html>'

Both $(html).find('#container') and $(html).find('div') seem to return [].

I have tried using .find() and .filter() to do this, but to no avail.

Upvotes: 4

Views: 2706

Answers (1)

James Montagne
James Montagne

Reputation: 78630

This is because when the html is parsed jquery is stripping the html and body tags. If you inspect the result, you will see a jquery object with 3 elements, header, div and footer. As a result, you can do this:

$(html).filter('#container')

http://jsfiddle.net/7AfsW/

Upvotes: 3

Related Questions