Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#11231 closed enhancement (fixed)

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:


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

Change History (13)

comment:1 Changed 11 years ago by sindresorhus

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.

comment:2 Changed 11 years ago by anonymous

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

comment:3 Changed 11 years ago by dmethvin

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.

comment:4 Changed 11 years ago by dmethvin

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.

comment:5 Changed 11 years ago by anonymous

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

comment:6 Changed 11 years ago by ajpiano

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") ];


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.

comment:7 Changed 11 years ago by anonymous

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.

comment:8 Changed 11 years ago by Rick Waldron

Keywords: needsdocs added

comment:9 Changed 11 years ago by dmethvin

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.

comment:10 Changed 11 years ago by sindresorhus

Priority: undecidedlow

comment:11 Changed 11 years ago by Sindre Sorhus

Resolution: patchwelcomefixed

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

Closes gh-666, thanks to @rkatic!

Changeset: ea9ec9527647f0cea396ca536e6f34b4a4491d11

comment:12 Changed 11 years ago by dmethvin

Milestone: None1.8

comment:13 Changed 11 years ago by mikesherov

Keywords: needsdocs removed
Note: See TracTickets for help on using tickets.