Acute Acute - 18 days ago 6
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 = $('div.page#test'); // 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

Use .find() http://api.jquery.com/find

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