Skip to main content

Bug Tracker

Side navigation

#13567 closed bug (notabug)

Opened March 05, 2013 03:29PM UTC

Closed March 05, 2013 03:39PM UTC

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

Reported by: mblase75@gmail.com Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: git
Keywords: Cc:
Blocked by: Blocking:
Description

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).

Attachments (0)
Change History (1)

Changed March 05, 2013 03:39PM UTC by dmethvin comment:1

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.