tags:

views:

26

answers:

2

I am trying to lookup elements within a dynamically created element. For some reason this fails. Why does the following return "0"? How to fix this?

  alert($('<p id="aa">xxx</p>').find("#aa").length);
A: 

.find() looks inside the child elements of the currently selected element.

Description: Get the descendants of each element in the current set of matched elements, filtered by a selector.

In this case, there are no nested elements, only text.

Dominic Barnes
+1  A: 
$('<p id="aa">xxx</p>').find("#aa") // looks inside the element you're holding

you want to filter based on the elements in your existing collection:

$('<p id="aa">xxx</p>').filter("#aa")

and thus

alert( $('<p id="aa">xxx</p>').filter("#aa").length );
Paul Irish