Skip to main content

Bug Tracker

Side navigation

#11231 closed enhancement (fixed)

Opened January 26, 2012 11:41AM UTC

Closed January 26, 2012 05:12PM UTC

Last modified October 15, 2012 08:56PM UTC

Append, Prepend, After, Before should accept an array as first argument

Reported by: anonymous Owned by:
Priority: low Milestone: 1.8
Component: manipulation Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:
Description

See https://plus.google.com/115102990534359458144/posts/VsiQkVsipBP for discussion, test cases, use case, and one-line fix.

Attachments (0)
Change History (13)

Changed January 26, 2012 12:34PM UTC by sindresorhus comment:1

Don't really see the point of this, you can already use DOM elements, jQuery objects, HTML strings, and arrays of DOM elements.

You can also use .apply to use an array of jQuery objects $('div').append.apply( $('div'), arrayOfjQueryObjects );

Alternatively you could just push the <option> elements to an array, use .join(''), and then pass it to .append as an html string.

Voting to close this.

Changed January 26, 2012 04:57PM UTC by anonymous comment:2

That's the point You can't use an array of *jQuery* objects. You shouldn't have to pull out apply just to do an append.

The claim that you can use append on "arrays of DOM elements" is simply incorrect. You can append a singular jQuery-wrapped collection of entities as a single jQuery object, yes, but that is not the same thing as an array. Given that $('body').append(['<div/>','<div/>','<div/>']), $('body').append([$('<div/>'),$('<div/>'),$('<div/>')]), and $('body').append([$('a'),$('b'),$('center')]) all fail with "Error: NOT_FOUND_ERR: DOM Exception 8", this claim is more than a bit overbroad.

The alternative isn't a true alternative, either; " pass it to .append as an html string" - I can't attach data(), event handlers, etc. to an html string; I likely wouldn't bother creating an array of jQuery objects if I could just keep the html string all together and just create a single jQuery object.

A quick check of Google leads me to think I'm not alone in this; Googling 'jQuery "append.apply"' gives around 3300 hits, and at least the first few pages' worth *all* are people looking to use append on an array of jQuery objects. The fix is so simple and short, there's demonstrated demand, and it would bring jQuery into agreement with that "arrays of DOM elements" claim of the docs...

Changed January 26, 2012 05:12PM UTC by dmethvin comment:3

resolution: → patchwelcome
status: newclosed
The fix is so simple and short

If you'd like to create a patch and test cases that allow .append() to accept additional reasonable inputs, submit it and we'll consider it. As I understand the proposal in that thread, it is not something we'd want to do. That is we don't want a new .appendAll() method that requires a single array as an argument.

Be aware, however, that we are being very careful with our bloat nowadays so the fix would indeed need to be simple and short.

Changed January 26, 2012 06:21PM UTC by dmethvin comment:4

component: unfiledmanipulation
type: bugenhancement

Oh and to be clear, the ticket is asking for JavasScript Arrays of non-DOM elements to be appendable. Arrays of HTML-ish strings and jQuery objects are not currently supported, but are also not documented to be supported so this is clearly an enhancement and not a bug in docs.

Changed January 26, 2012 07:15PM UTC by anonymous comment:5

The suggestion is that arrays be usable as arguments for append(), etc. The appendAll plugin is just a for-now workaround, aliasing append.apply.

Regarding the documentation:

Could you explain then what the second argument in http://api.jquery.com/append/ is talking about when it says "...arrays of elements...", because that's quite clearly documented as supported.

It's also not the only place that arrays are stated to be supported.

(There's also mentions of arrays being supported scattered on those 4 pages, outside of the formal definitions.)

Yet, as I mentioned earlier, regardless of the type of contents of the array, between append, prepend, after, and before, using an array as argument in any of these 5 places will only result in a "Error: NOT_FOUND_ERR: DOM Exception 8".

Changed January 26, 2012 07:27PM UTC by ajpiano comment:6

By "array of Elements" it means exactly that. A simple JavaScript array whose contents are simple, unwrapped DOM Element instances. For instance:

var arr = [ document.getElementById("foo"), document.getElementById("bar") ];

Or

var arr = $("input").get() // or .toArray

The examples you provided in comment 2 are an array of strings, an array of jquery objects, and another array of jquery objects, respectively.

Changed January 26, 2012 07:36PM UTC by anonymous comment:7

So an array of unwrapped DOM Element instances is ok, but an array of html strings or jQuery objects (ie, wrapped DOM Element instances) is not? That's far more specific than "arrays of elements"; that wording implies at least that arrays of wrapped elements should be fine, if not also html strings (for consistency with the rest of jQuery). That this even is present doesn't make a lot of sense; where else in jQuery is direct DOM Element manipulation as argument to jQuery found? Off the top of my head, nowhere - and these 4 functions accepting them, but not other quite reasonable/expectable contents of arrays, seems just plain bizarre.

Re: patches, there's at least three suggested ones already in the bugtracker.

http://bugs.jquery.com/ticket/8897 has a duck punched version

http://bugs.jquery.com/ticket/9011 has https://github.com/xavierm02/jquery/commit/20e17ef9d9d40057524aa15858048a67189574b1

(and there's at least one other, though I'm not finding it atm).

Just to be complete, I'll also include the other related/dupe tickets here, since I can't link them more formally to this ticket:

http://bugs.jquery.com/ticket/9647

http://bugs.jquery.com/ticket/8466

http://bugs.jquery.com/ticket/7066

http://bugs.jquery.com/ticket/10310

I'd note that each one of these tickets, in the comments, says something different about what the docs say, and what jQuery accepts re: arrays of elements. As mentioned above, though, none of the ways they suggest arrays passed as arguments should work does actually work.

Changed January 26, 2012 07:40PM UTC by rwaldron comment:8

keywords: → needsdocs

Changed January 26, 2012 07:42PM UTC by dmethvin comment:9

Thanks for the literature search, but the reason I closed the ticket as patchwelcome and not a duplicate was that it was implied that there was a simple and robust solution. If there is, I am very willing to consider it because as you mention it has been requested several times.

I don't want to search through a bunch of tickets with "solutions" and try to judge whether they are good or not, then try to craft working code and test cases. I was asking for a patch. Or we can close this as a dup of of #8897.

Changed February 09, 2012 04:29PM UTC by sindresorhus comment:10

priority: undecidedlow

Changed June 16, 2012 02:34AM UTC by Sindre Sorhus comment:11

resolution: patchwelcomefixed

Fix #11231, (append|prepend|before|after) w/ array of jQuery objects.

Closes gh-666, thanks to @rkatic!

Changeset: ea9ec9527647f0cea396ca536e6f34b4a4491d11

Changed June 16, 2012 02:36AM UTC by dmethvin comment:12

milestone: None1.8

Changed October 15, 2012 08:56PM UTC by mikesherov comment:13

keywords: needsdocs