Bug Tracker

Ticket #5870 (closed bug: invalid)

Opened 5 years ago

Last modified 4 years ago

Specifying context fails for selector when context is a wrapped string

Reported by: tvanfosson Owned by:
Priority: minor Milestone: 1.4
Component: core Version: 1.4
Keywords: context selector Cc:
Blocking: Blocked by:

Description

When checking a string for the existence of an element by specifying the string as a wrapped jQuery as the context, the selector fails.

var html = '<div>test</div>'; var exists = $('div', $(html)).length > 0;

This code results in exists === false, when clearly the the string contains a valid divelement.

Yet, if the element is added to the document (when it previously contained no divs), it does properly find the element

$('body').append(html); var exists2 = $('div').length > 0;

Attachments

exists.htm Download (395 bytes) - added by tvanfosson 5 years ago.
sample html

Change History

Changed 5 years ago by tvanfosson

sample html

comment:1 Changed 5 years ago by dmethvin

$(x,y) is equivalent to $(y).find(x).

So, $('div', $('<div>test</div>')) is the same as $(<div>test</div>).find('div').

The .find() method looks *below* the nodes provided. There are no div elements *contained* in the context. See the documentation for jQuery(), .find(), and .filter():

 http://api.jquery.com/jQuery/

comment:2 Changed 4 years ago by addyosmani

  • Status changed from new to closed
  • Resolution set to invalid

Invalid: I believe the explanation offered by dmethvin satisfactorily explains why this particular bug isn't actually a valid jQuery core issue.

Further information regarding your original concerns can be addressed by looking at the API documentation site as previously suggested.

Closing as there has been no further activity on this ticket for a long time and it appears to have been resolved.

Note: See TracTickets for help on using tickets.