Compare by attribute

Sort an array of HTML elements.

// Create a sort function, which sorts by attribute.
var compareByAttr = function(attrName) {
    return function(leftElement, rightElement) {
        var leftCalcOrder = Number($(leftElement).attr(attrName));
        var rightCalcOrder = Number($(rightElement).attr(attrName));
        return leftCalcOrder - rightCalcOrder;
    };
};
// Normal HTML usage:
document.getElementsByTagName('input').sort(compareByAttr('tabindex'))
// jQuery example:
$('a,input,select,button').sort(compareByAttr('tabindex'))
 
 
blog\compare_by_attribute.txt · Last modified: 2010-07-02T09:42
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki