Created
October 1, 2011 20:23
-
-
Save pete-otaqui/1256610 to your computer and use it in GitHub Desktop.
jQuery plugin to append content at a specific or random index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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