Bug Tracker

Ticket #8810 (closed bug: invalid)

Opened 3 years ago

Last modified 3 years ago

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:
Blocking: Blocked by:

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

comment:1 Changed 3 years ago by timmywil

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

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:2 Changed 3 years ago by timmywil

comment:3 Changed 3 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 3 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.