Skip to content

Instantly share code, notes, and snippets.

@marcuswestin
Created June 18, 2012 23:57
Show Gist options
  • Select an option

  • Save marcuswestin/2951521 to your computer and use it in GitHub Desktop.

Select an option

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

malammar commented Aug 7, 2012

Copy link
Copy Markdown

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