Skip to content

Instantly share code, notes, and snippets.

@xuncheng
Created January 2, 2014 14:02
Show Gist options
  • Save xuncheng/8219581 to your computer and use it in GitHub Desktop.
Save xuncheng/8219581 to your computer and use it in GitHub Desktop.
Find the next or the previous instance for a given selected range
jQuery.fn.elementAfter = function(other) {
for(i = 0; i < this.length - 1; i++) {
if (this[i]() == other) {
return jQuery(this[i + 1]());
}
}
};
jQuery.fn.elementBefore = function(other) {
if (this.length > 0) {
for(i = 1; i < this.length; i++) {
if (this[i]() == other) {
return jQuery(this[i - 1]());
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment