Ticket #7853 (closed bug: fixed)
`context` issue (incompatible change) when using DOM elements with $
|Reported by:||T.J. Crowder||Owned by:||dmethvin|
As of 1.4.x, using a DOM element with $() apparently sets the jQuery object's context property, which changes the behavior of functions like add incompatibly with v1.3.x.
These two lines, which look like they should be identical, do very different things:
var a = $('#first').add('li');
var b = $(document.getElementById('first')).add('li');
The first line finds the element with the ID first and adds any lis in the document to it.
The second line finds the element with the ID first and adds only lis within that element.
Naturally this has a pretty big effect on event handlers, where $(this) (with this being a DOM element) is a very common use case.
This behavior is very surprising. People will expect the two lines above to do the same thing. I've seen people saying those two uses of $() would be functionally the same all over the place, and of course they were the same prior to 1.4.0.
This looks and acts very much like a bug, but 1.4.0 came out nearly a year ago and the issue has existed from then through to 1.4.4 (the latest as of this writing), which makes me wonder if it's an intentional compatibility-breaking change. If so, it's unfortunate (IMHO, but I'm not involved with jQuery's development). It's also not mentioned in the release notes. In any case, if it remains, it needs substantial discussion in the documentation, not least the `jQuery()` documentation, `add` documentation, and anywhere else this.context will have this impact. But unless it was a very carefully considered change, strongly recommend reverting it and dealing with the places that were relying on it (live, I suspect) another way.
Details: The underlying cause of the behavior is that jQuery.fn.init sets the context to the DOM element (line 110 of v1.4.4; currently line 91 of core.js), and then add uses this.context (line 4,459 of jQuery.js v1.4.4; currently line 137 of traversing.js). It's probably used elsewhere as well.
- Priority changed from undecided to high
- Status changed from new to open
- Component changed from unfiled to traversing
- Owner set to dmethvin
- Status changed from open to assigned