Skip to content

Commit 17f0e26

Browse files
dwilson6timmywil
authored andcommitted
Event: Fix chaining .on() with null handlers
Fixes gh-2846
1 parent 780cac8 commit 17f0e26

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

‎src/event.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ function on( elem, types, selector, data, fn, one ) {
7070
}
7171
if ( fn === false ) {
7272
fn = returnFalse;
73+
} else if ( !fn ) {
74+
return elem;
7375
}
7476

7577
if ( one === 1 ) {

‎test/unit/event.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ QUnit.module( "event", {
55
teardown: moduleTeardown
66
} );
77

8+
QUnit.test( "null or undefined handler", function( assert ) {
9+
assert.expect( 4 );
10+
11+
// Supports Fixes bug #7229
12+
try {
13+
jQuery( "#firstp" ).on( "click", null );
14+
assert.ok( true, "Passing a null handler will not throw an exception" );
15+
} catch ( e ) {}
16+
17+
try {
18+
jQuery( "#firstp" ).on( "click", undefined );
19+
assert.ok( true, "Passing an undefined handler will not throw an exception" );
20+
} catch ( e ) {}
21+
22+
var expectedElem = jQuery( "#firstp" );
23+
var actualElem = expectedElem.on( "click", null );
24+
assert.equal(actualElem, expectedElem, "Passing a null handler should return the original element");
25+
26+
actualElem = expectedElem.on( "click", undefined );
27+
assert.equal(actualElem, expectedElem, "Passing a null handler should return the original element");
28+
} );
29+
830
QUnit.test( "on() with non-null,defined data", function( assert ) {
931

1032
assert.expect( 2 );

0 commit comments

Comments
 (0)