If I run this code -
var html= '<html><head></head><body><div class="bar"></div></body></html>';
console.log($(html).find('div'));
I get no results returned, if I run this code -
var html= '<html><head></head><body><div><div class="bar"></div></div></body></html>';
console.log($(html).find('div'));
Then I get a single result returned - the inner div (<div class="bar"></div>
). I would have expected the first code snippet to return a single result and the second snippet two results.
Similarly, this code returns no results -
var code = $("<div id='foo'>1</div><div id='bar'>2</div>");
console.log(code.find('div'));
but this code alerts 'div' twice -
var code = $("<div id='foo'>1</div><div id='bar'>2</div>");
code.each(function() {
alert( this.nodeName );
})
Given the result of the second snippet, I would have expected the first code snippet to return two results. Could someone please explain why I'm getting the results I'm getting?