Ticket #5870 (closed bug: invalid)
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
Change History
Changed 3 years ago by tvanfosson
-
attachment
exists.htm
added
comment:1 Changed 3 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():
comment:2 Changed 3 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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

sample html