Side navigation
#3602 closed bug (worksforme)
Opened November 13, 2008 11:58PM UTC
Closed June 20, 2010 08:13PM UTC
Passing a collection with a null value will retun a jQuery with window in its selection.
Reported by: | rolandpoulter | Owned by: | flesler |
---|---|---|---|
Priority: | trivial | Milestone: | 1.3 |
Component: | core | Version: | 1.2.6 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Lets say I have function with two arguments, the first being required and the second being optional:
function (req, opt) {}
Lets say that both arguments are expected when set to be an element, jquery obj, selector str, etc. Something jQuery can use.
In this function I might want to bind events to both of the passed in elements.
function (elem1 /*req*/, elem2 /* opt */) {
$([elem1, elem2]).bind('click', function () {
// do something...
});
}
Now this would work great as long as the optional element is not null. But when it is undefined, it the same as binding the event to the window.
$([elem, null /* which is the same as window
*/]).bind('click', function () {return false;});
I think null's in an array should be ignored, instead of being turned into the equivilent of $(window). Otherwise I have to expand out this sort of code into something less ideal. Such as:
function (elem1, elem2) {
var arr = [elem1];
if (elem2) arr.push(elem2);
$(arr).bind('click', function () {/* do something */});
}
Which isn't so bad, but was annoying.
PS. This code is an simplified example, of the problem I encountered due to this problem. In this example the simplest solution would have been to use arguments
instead of making an array. But that would not have worked for my situation.
Attachments (2)
Change History (6)
Changed November 14, 2008 12:50AM UTC by comment:1
component: | unfilled → core |
---|---|
resolution: | → invalid |
status: | new → closed |
Changed November 14, 2008 07:27PM UTC by comment:2
resolution: | invalid |
---|---|
status: | closed → reopened |
Sorry, I only meant to attach it once.
Changed November 15, 2008 02:51PM UTC by comment:4
Nope. This isn't the same situation.
Changed December 07, 2009 03:42AM UTC by comment:5
To solve the original problem, why not just pass the arguments list instead of the two args individually?
$(arguments).bind(...)
If nulls aren't going to be allowed in real Arrays passed to $() it seems like they should be consistently scrubbed out anywhere they might be passed in, such as in .add. Maybe .unique or .merge is the place to discard them? Any method that goes through jQuery.clean already has null/undefined elements thrown out.
Changed June 20, 2010 08:13PM UTC by comment:6
resolution: | → worksforme |
---|---|
status: | reopened → closed |
The change to 1.4 that made jQuery(null) --> jQuery([]) may have inadvertently fixed some of these cases. However, I think passing in a null inside an array is an error on the part of the caller. The signature in question is
jQuery(elementArray)and null certainly isn't a DOM element.
$([foo,null]) simply generates [foo, null]. It is not replaced by the window.
Data doesn't get parsed when passed withing an array to jQuery.
Do reopen with a valid test case if you want (an html file).