I remember struggling with this before but I’ve googled and can’t find the reference. I want to:
var x = $('<div class="y">xxx</div>');
x.find('.y'); // returns 0 elements
and expect to find 1 element but find 0. was it about being attached to the document? I tried it with similar failure:
var x = $('<div class="y">xxx</div>');
$(document).append(x);
alert($(document).find('.y').length); // also returns 0 elements
what gives?
This doesn’t work because what you’re telling it to do is find an element with class
ywithin the newly created element when the element itself is what you’re looking for.Here is something that works: