Skip to content

Commit 78420d4

Browse files
authored
Core: Implement .even() & .odd() to replace POS :even & :odd
`:even` & `:odd` are deprecated since jQuery 3.4.0 & will be removed in 4.0.0. The new `even()` & `odd()` methods will make the migration easier. Closes gh-4485
1 parent f810080 commit 78420d4

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/core.js

+12
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ jQuery.fn = jQuery.prototype = {
100100
return this.eq( -1 );
101101
},
102102

103+
even: function() {
104+
return this.pushStack( jQuery.grep( this, function( _elem, i ) {
105+
return ( i + 1 ) % 2;
106+
} ) );
107+
},
108+
109+
odd: function() {
110+
return this.pushStack( jQuery.grep( this, function( _elem, i ) {
111+
return i % 2;
112+
} ) );
113+
},
114+
103115
eq: function( i ) {
104116
var len = this.length,
105117
j = +i + ( i < 0 ? len : 0 );

test/unit/core.js

+12
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,18 @@ QUnit.test( "first()/last()", function( assert ) {
648648
assert.deepEqual( $none.last().get(), [], "last() none" );
649649
} );
650650

651+
QUnit.test( "even()/odd()", function( assert ) {
652+
assert.expect( 4 );
653+
654+
var $links = jQuery( "#ap a" ), $none = jQuery( "asdf" );
655+
656+
assert.deepEqual( $links.even().get(), q( "google", "anchor1" ), "even()" );
657+
assert.deepEqual( $links.odd().get(), q( "groups", "mark" ), "odd()" );
658+
659+
assert.deepEqual( $none.even().get(), [], "even() none" );
660+
assert.deepEqual( $none.odd().get(), [], "odd() none" );
661+
} );
662+
651663
QUnit.test( "map()", function( assert ) {
652664
assert.expect( 2 );
653665

0 commit comments

Comments
 (0)