Skip to content

Instantly share code, notes, and snippets.

@pete-otaqui
Created October 1, 2011 20:23
Show Gist options
  • Save pete-otaqui/1256610 to your computer and use it in GitHub Desktop.
Save pete-otaqui/1256610 to your computer and use it in GitHub Desktop.
jQuery plugin to append content at a specific or random index
jQuery.fn.appendAt = function( content, index ) {
this.each(function(i, item) {
var $content = $(content).clone();
if ( index === 0 ) {
$(item).prepend($content);
} else {
$content.insertAfter($(item).children().eq(index-1));
}
});
$(content).remove();
return this;
};
jQuery.fn.appendAtRandom = function( content ) {
this.each(function(i, item) {
var max = $(item).children().length,
pos = Math.floor( Math.random() * (max+1) );
$(item).appendAt(content, pos);
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment