Posted By

kouphax on 10/06/08


Tagged

javascript sort jquery


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wirenaught


inDOMOrder jQuery Plugin


 / Published in: JavaScript
 

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).

  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  

You need to login to post a comment.