Why doesn't this work?
var string = '<b>Hello</b> this is just a <b>test</b>';
console.log($(string).find('b'));
I would expect Firebug's console to give me the two <b>
elements, but instead I get an empty array.
However, if I change it to this:
var string = '<b>Hello</b> this is just a <b>test</b>';
console.log($('<div/>').html(string).find('b'));
It does what I want. I understand that jQuery internally assigns passed HTML it can't handle via createElement
to a <div>
element in order to parse it into a DOM structure, but shouldn't it be able to find elements in this passed HTML like I tried above? To make matters more confusing, sometimes I see people successfully using the first syntax when they get returned (and sometimes malformed) HTML from an AJAX request, but this never seems to work for me without manually appending my HTML to a <div>
What am I missing?