Bug Tracker

Opened 11 years ago

Closed 9 years ago

#3602 closed bug (worksforme)

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)

bug.html (2.3 KB) - added by rolandpoulter 11 years ago.
Test Case
bug.2.html (2.3 KB) - added by rolandpoulter 11 years ago.
Test Case

Download all attachments as: .zip

Change History (8)

comment:1 Changed 11 years ago by flesler

Component: unfilledcore
Resolution: invalid
Status: newclosed

$([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).

Changed 11 years ago by rolandpoulter

Attachment: bug.html added

Test Case

Changed 11 years ago by rolandpoulter

Attachment: bug.2.html added

Test Case

comment:2 Changed 11 years ago by rolandpoulte

Resolution: invalid
Status: closedreopened

Sorry, I only meant to attach it once.

comment:3 Changed 11 years ago by davidserduke

Is this essentially a duplicate of #1807?

comment:4 Changed 11 years ago by flesler

Nope. This isn't the same situation.

comment:5 Changed 10 years ago by dmethvin

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.

comment:6 Changed 9 years ago by dmethvin

Resolution: worksforme
Status: reopenedclosed

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.

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

Note: See TracTickets for help on using tickets.