Go through a regular html line using jQuery

I cannot cross the user html string with jquery, as 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 expect to get 1

I suspect I'm doing something really stupid here, but after a few hours I still don't understand what happened.

+4
source share
1 answer

You need to put your HTML in the β€œroot element” as a DIV:

 $("<div>"+html+"</div>").find("a").length 
+8
source

Source: https://habr.com/ru/post/1300556/


All Articles