Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 884
moriesta Avatar asked Mar 10 '14 18:03

moriesta


People also ask

What does prepend() do?

prepend() method inserts a set of Node objects or string objects before the first child of the Element . String objects are inserted as equivalent Text nodes.

How to prepend element in jQuery?

jQuery prepend() MethodThe prepend() method inserts specified content at the beginning of the selected elements. Tip: To insert content at the end of the selected elements, use the append() method.

How to append an element as a first child in jQuery?

prepend() method inserts the specified content as the first child of each element in the jQuery collection (To insert it as the last child, use . append() ).

How to append before div in jQuery?

jQuery insertBefore() Method The insertBefore() method inserts HTML elements before the selected elements. Tip: To insert HTML elements after the selected elements, use the insertAfter() method.


1 Answers

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

var page = $('div#pages').find('div.page#test'); 
like image 90
CRABOLO Avatar answered Sep 20 '22 04:09

CRABOLO