Bug Tracker

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#8810 closed bug (invalid)

Cannot access top level element of a parsed string

Reported by: jefferey.sutherland@… 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.

Change History (4)

comment:1 Changed 5 years ago by timmywil

  • Component changed from unfiled to selector
  • Resolution set to invalid
  • Status changed from new to closed

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/

comment:3 Changed 5 years ago by jefferey.sutherland@…

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.

comment:4 Changed 5 years ago by timmywil

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.

Note: See TracTickets for help on using tickets.