richb
richb

Reputation: 4926

Newbie jQuery question, creating DOM elements on the fly

In the following jQuery snippet, why does the foo2 object have no elements? My alert shows me that foo1 does contain DOM elements. Shouldn't find("*") then just return what is effectively a clone of foo1?

var foo1 = $("<div id='content'>HAGGIS</div>");  
var foo2 = foo1.find("*");

alert("foo1("+foo1.length+"): "+$('<div>').append(foo1.clone()).remove().html()  
    + "\n\n" +  
      "foo2("+foo2.length+"): "+$('<div>').append(foo2.clone()).remove().html());

Upvotes: 1

Views: 122

Answers (3)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196296

From the jQuery documentation about .find()

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

foo1 has no descendants.

Upvotes: 2

Reigel Gallarde
Reigel Gallarde

Reputation: 65284

No, if I understand correctly your question, then I'd say that find() will look for the children. That is why foo2.length will give you 0. If you want the clone of foo1 then var foo2 = foo1.clone(); would be the right one.

Upvotes: 1

alex
alex

Reputation: 490647

foo2 has no elements because the selector is for all children of foo1, which has no child elements, just a text node.

Upvotes: 3

Related Questions