[ACCEPTED]-Opposite of jQuery's :eq()-jquery-selectors

Accepted answer
Score: 76

Use not:

$('li').not(':eq(2)');

0

Score: 10

Alright, it's just

$("li:not(:eq(2))");

0

Score: 7

The other answers will work just fine, but 6 as an alternative you could implement you 5 own custom selector for neq

$.extend($.expr[":"], {  
    neq: function(elem, i, match) {  
        return i !== (match[3] - 0);
    }  
});  

And then you could 4 do what you originally suggested.

$("li:neq(2)").size();

Although 3 another post suggested using .length instead of 2 .size, which will be better as its just a property 1 and not an extra function call.

$("li:neq(2)").length;
Score: 6

I would use filter for such case,

$('li').filter(function (i, item) {
   return i != 2;
})

0

Score: 1

In addition to the custom selector, you 1 could also implement this as a jQuery plugin:

$.fn.neg = function (index) {
    return this.pushStack( this.not(':eq(' + index + ')') );
}

More Related questions