Bug Tracker

Opened 10 years ago

Closed 10 years ago

#13567 closed bug (notabug)

error in .map() when callback returns a jQuery object

Reported by: [email protected] Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: git
Keywords: Cc:
Blocked by: Blocking:


http://jsfiddle.net/mblase75/NfzbA/ -- jQuery 1.9.1

var $opts = $('.plant-page').map(function (i, el) {
    return $('<option>');
}).appendTo('#change-page select');

error is: "Uncaught TypeError: Cannot read property 'ownerDocument' of undefined"

When I switch to jQuery 1.8.3, result is as expected -- the option elements are appended to the select: http://jsfiddle.net/mblase75/NfzbA/2/

in jQuery 1.9.1, if I rewrite the .map() callback function to return a DOMelement instead of a jQuery object, it works as expected: http://jsfiddle.net/mblase75/NfzbA/3/

var $opts = $('.plant-page').map(function (i, el) {
    return $('<option>').text(this.id)[0];
}).appendTo('#change-page select');

The documentation at http://api.jquery.com/map/ doesn't seem to say that the callback function may not return jQuery objects, so I'm not sure if this is a bug in 1.9 or a reversion to intended behavior (which should be clarified in the documentation).

Change History (1)

comment:1 Changed 10 years ago by dmethvin

Resolution: notabug
Status: newclosed

You are trying to append a jQuery object that is inside a jQuery object, since you inserted a jQuery object into said jQuery object via the $.fn.map(). This is not supported, but obvious enough. We generally don't make a list of everything that cannot be in a jQuery set because there is an infinite list of possibilities for doing it wrong. Your rewrite looks better.

Note: See TracTickets for help on using tickets.