Acute Acute - 1 year ago 84
Javascript Question

jQuery: Can't select just appended element

This is what happens on event:

$('div#pages').append($('<div class="page" id="test">...</div>'));

And then, on another event it fails doing this:

var page = $(''); // returns empty array

I've debugged, and the appended html appears in the document structure after appending, but fails to get selected. Doing the same in browser console works perfectly.

What could be the problem?

Answer Source

Use .find()

var page = $('div#pages').find('');