Bug Tracker

Modify

Ticket #3602 (closed bug: worksforme)

Opened 5 years ago

Last modified 3 years ago

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:
Blocking: Blocked by:

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

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

Change History

comment:1 Changed 5 years ago by flesler

  • Status changed from new to closed
  • Resolution set to invalid
  • Component changed from unfilled to core

$([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 5 years ago by rolandpoulter

Test Case

Changed 5 years ago by rolandpoulter

Test Case

comment:2 Changed 5 years ago by rolandpoulte

  • Status changed from closed to reopened
  • Resolution invalid deleted

Sorry, I only meant to attach it once.

comment:3 Changed 5 years ago by davidserduke

Is this essentially a duplicate of #1807?

comment:4 Changed 5 years ago by flesler

Nope. This isn't the same situation.

comment:5 Changed 3 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 3 years ago by dmethvin

  • Status changed from reopened to closed
  • Resolution set to worksforme

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/

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.