xxx

'). Find ("# aa"). Length); I am trying to search for elements inside a...">

JQuery.find for newly created elements alert ($ ('<p id = "aa"> xxx </p>'). Find ("# aa"). Length);

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

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

      

+2


a source to share


2 answers


$('<p id="aa">xxx</p>').find("#aa") // looks inside the element you're holding

      

you want to filter based on items in your existing collection:

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

      



and therefore

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

      

+3


a source


.find()

loops through the children of the currently selected item.

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



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

0


a source







All Articles