Skip to content

Instantly share code, notes, and snippets.

@xilin
Created December 20, 2013 07:37
Show Gist options
  • Save xilin/8051573 to your computer and use it in GitHub Desktop.
Save xilin/8051573 to your computer and use it in GitHub Desktop.
Compare DOM tree order of elements
// http://stackoverflow.com/questions/3860351/relative-position-in-dom-of-elements-in-jquery
// What this does is .add() the additional element (or selector)
// (which jQuery keeps in document order) and then checks if it's the second of the two.
(function($) {
$.fn.isBefore = function(elem) {
if(typeof(elem) == "string") elem = $(elem);
return this.add(elem).index(elem) > 0;
}
})(jQuery)
$("#p1").isBefore("#p2");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment