Getting the “match” object in a Custom Filter Selector in jQuery 1.8

jQuery has added a utility for creating custom pseudos in Sizzle. It’s a little more verbose, but it’s much more readable than using match[3]. It also has the advantage of being more performant as you can avoid repeating tedious calculations every time an element is tested. The answer that has already been accepted is a good answer, but let me add a note to say that you can use $.expr.createPseudo instead of setting the sizzleFilter property yourself, which will save a little space.

jQuery.expr[':'].tabIndexAbove = $.expr.createPseudo(function( tabindex ) {
    return function(elem) {
        return +elem.getAttribute('tabindex') > tabindex;
    }
});

$('input:tabIndexAbove(4)').css('background', 'teal');

jsfiddle: http://jsfiddle.net/timmywil/YCsCm/7/

This is all documented on Sizzle’s github:
https://github.com/jquery/sizzle/wiki/Sizzle-Documentation

Leave a Comment