I want to parse the html string for a jQuery object, then find the element by id.
I tried 3 options, but only the latter works. I do not know why others do not work?
var html = "<html><body><div id='main'></div></body></html>"; // Not work, return 0 console.log($(html).find('#main').length); // Not work, return 0 console.log($($.parseHTML(html)).find('#main').length); // Works, return 1 console.log($("<html/>").html(html).find('#main').length);
Here is an example: http://jsfiddle.net/nbyofkam/2/
javascript jquery
andyf
source share