Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traverse a custom html string with Jquery

I am not able to traverse a custom html string with Jquery, like in this example:

html = '<a href="http://www.site.com"><img width="800" src="http://www.site.com/pic.jpg" alt="" /></a><br /><br />Description<br />';
found = $(html).find("a").length;

"found" returns 0, while I would expect to get 1

I suspect I'm doing something really stupid here, but after hours I still don't see what's wrong.

like image 850
Omiod Avatar asked Feb 28 '23 15:02

Omiod


1 Answers

You need to put your HTML code into a “root element” like a DIV:

$("<div>"+html+"</div>").find("a").length
like image 124
Gumbo Avatar answered Mar 12 '23 06:03

Gumbo