Skip to content

Instantly share code, notes, and snippets.

@nextend
Forked from xilin/isBefore.js
Created May 15, 2017 14:12
Show Gist options
  • Save nextend/3d2422d8ba7f24257917e702de762565 to your computer and use it in GitHub Desktop.
Save nextend/3d2422d8ba7f24257917e702de762565 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