inDOMOrder jQuery Plugin


/ Published in: JavaScript
Save to your folder(s)

jQuery Plugin for returning selected DOM Nodes in DOM order. e.g. $('img.L,img.Y') will not be guaranteed DOM order (usually all the L's then all the Y's).

Currently only works with IE and FF (that's all I needed).


Copy this code and paste it in your HTML
  1. (function($){
  2.  
  3. $.fn.sort = function() {
  4. return this.pushStack($.makeArray( [].sort.apply(this, arguments)));
  5. };
  6.  
  7. function DOMOrderComparator(a,b){
  8. if($.browser.msie){
  9. return a.sourceIndex - b.sourceIndex;
  10. }else if($.browser.mozilla){
  11. return 3 - (a.compareDocumentPosition(b) & 6);
  12. }
  13. }
  14.  
  15. $.fn.inDOMOrder = function(){
  16. return this.sort(DOMOrderComparator)
  17. }
  18. })(jQuery);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.