Exception thrown when running `find` in a non-attached DOM node
|Reported by:||jquery@…||Owned by:||gibson042|
The following snippet throws an exception in jQuery from version 1.8.3 and forward:
$('<div><a></a></div>').find(':not(:hidden) + c , d')
The exception reads:
Cannot read property '0' of undefined
When asking JSFiddle to use jQuery version 1.x/2.x, the message changes to:
Cannot read property 'length' of undefined
If appending the new element to another node, it works fine:
$('<div><a></a></div>').appendTo('<div>').find(':not(:hidden) + c , d')
Testing in JSFiddle (http://jsfiddle.net/cpxbb/), it worked in jQuery 1.7.2 and started failing in jQuery 1.8.3.
The error message changed nature between 1.9.1/2.0.2 and the 1.x/2.x versions (JSFiddle does not currently have any specific versions above 1.9.1/2.0.2).
I debugged a bit through the minified version of 1.8.3, and it fails at a point where it tries to find tags based on the parentNode of the root element, which is a document-fragment for an unattached element.
The call that returned undefined is:
i.find.TAG("*", h && a.parentNode || a)
where a is the top element (the div in the example code).
An immediate work-around is to attach the node to a dummy-element before running the find operation.
Change History (4)
comment:1 Changed 3 years ago by gibson042
- Owner set to gibson042
- Status changed from new to assigned
comment:2 Changed 3 years ago by Richard Gibson
- Resolution set to fixed
- Status changed from assigned to closed