Skip to content

Instantly share code, notes, and snippets.

@marcuswestin
Created June 18, 2012 23:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save marcuswestin/2951521 to your computer and use it in GitHub Desktop.
Monkey-patch jquery $('.foo').append(...) to accept variable number arguments and arrays of elements
var originalAppend = $.fn.append
$.fn.append = function() {
if (arguments.length == 1) {
var arg = arguments[0]
if ($.isArray(arg)) {
for (var i=0; i<arg.length; i++) { this.append(arg[i]) }
} else {
originalAppend.call(this, arg)
}
} else {
for (var i=0; i<arguments.length; i++) { this.append(arguments[i]) }
}
return this
}
// Now possible:
$('.foo').append('foo', 'bar', [1,2,3])
@malammar
Copy link

malammar commented Aug 7, 2012

Thanks! Very small piece of code for something that should definitely be in jQuery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment