/ Published in: JavaScript
This function will sort an array of DOM elements in order of the DOM position. Useful when using calls such as $$("img.L,img.M") which doesn;t return a sorted element array.
It uses prototype but you can replace the Prototype.Browser stuff with anything else.
It uses prototype but you can replace the Prototype.Browser stuff with anything else.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var DOMPositionComparator = function(a,b){ if(Prototype.Browser.IE){ return a.sourceIndex - b.sourceIndex; }else if(Prototype.Browser.Gecko){ return 3 - (a.compareDocumentPosition(b) & 6); } }; var sortedFields = $$('img.L,img.M').sort(DOMPositionComparator);