You need to pass the fragment of the string in jQuery and save the link to it:
var $html = $(document.createDocumentFragment()); var $fragment = $('<a href="#">First link</a><a href="#">Second link</a>'); $html.append($fragment); console.log($fragment.filter('a').length); console.log($html[0].querySelectorAll('a').length);
Then the analyzed fragment can be passed. Note that find here is replaced with filter , because we have a link to the collection of elements a , and find only works with children of the containing element.
This will result in logging 2 and 2 as expected. Demo: http://jsfiddle.net/fec9csxu/
source share