Skip to main content

Bug Tracker

Side navigation

#8810 closed bug (invalid)

Opened April 07, 2011 09:33PM UTC

Closed April 07, 2011 10:04PM UTC

Last modified April 07, 2011 11:57PM UTC

Cannot access top level element of a parsed string

Reported by: jefferey.sutherland@gmail.com Owned by:
Priority: undecided Milestone: 1.next
Component: selector Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:
Description

In Firebug on Firefox 4, the following happens.

Console input:

var t = $('<div><p>something</p></div>');

t.find("div");

Console output:

[]

Console input:

var t = $('<div><p>something</p></div>');

t.find("p");

Console output:

[p, p]

According to the documentation, t.find("div") should return the first DIV tag.

jQuery 1.5.1 produces the same behavior. Using Firefox 4, Firebug 1.7.0, and OS X 10.6.6.

Attachments (0)
Change History (4)

Changed April 07, 2011 10:04PM UTC by timmywil comment:1

component: unfiledselector
resolution: → invalid
status: newclosed

Thanks for taking the time to contribute to the jQuery project!

That is the correct behavior.

The docs on find states:

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

As you can see, find retrieves descendants. The div is not a descendant of itself. You can include the div as well by using http://api.jquery.com/andSelf:

http://jsfiddle.net/timmywil/sLw7a/

Changed April 07, 2011 10:14PM UTC by timmywil comment:2

Changed April 07, 2011 10:23PM UTC by jefferey.sutherland@gmail.com comment:3

The documentation you provide makes sense. However, it feels non-intuitive to not be able to grab the top-level element of an HTML snippet with the current finder syntax. Your second example makes more sense, but is still a conceptual leap.

Thanks for the tips.

Changed April 07, 2011 11:57PM UTC by timmywil comment:4

The point of the first example was to show you how to get the top element without it's children without having to use clone and empty. The point of the second example was to show you that you don't need to use find, or any other function. You already have the element you want; why call a function? Not really a conceptual leap there.