Skip to content

Commit 1fb2f92

Browse files
committed
Fix #3827. Get the correct checkbox status for a click handler.
1 parent 4fed8eb commit 1fb2f92

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/event.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ jQuery.event = {
263263

264264
// Allow special events to draw outside the lines
265265
special = jQuery.event.special[ type ] || {};
266-
if ( special.trigger && special.trigger.apply( elem, data ) === false ) {
266+
if ( !onlyHandlers && special.trigger && special.trigger.apply( elem, data ) === false ) {
267267
return;
268268
}
269269

@@ -523,7 +523,15 @@ jQuery.event = {
523523
// Prevent triggered image.load events from bubbling to window.load
524524
noBubble: true
525525
},
526-
526+
click: {
527+
// For checkbox, fire native event so checked state will be right
528+
trigger: function() {
529+
if ( jQuery.nodeName( this, "input") && this.type === "checkbox" && this.click ) {
530+
this.click();
531+
return false;
532+
}
533+
}
534+
},
527535
focus: {
528536
delegateType: "focusin"
529537
},

test/unit/event.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,35 @@ test("clone() delegated events (#11076)", function() {
28312831
clone.remove();
28322832
});
28332833

2834+
test("checkbox state (#3827)", function() {
2835+
expect( 9 );
2836+
2837+
var markup = jQuery("<div><input type=checkbox><div>").appendTo("#qunit-fixture"),
2838+
cb = markup.find("input")[0];
2839+
2840+
jQuery(cb).on( "click", function(){
2841+
equal( this.checked, false, "just-clicked checkbox is not checked" );
2842+
});
2843+
markup.on( "click", function(){
2844+
equal( cb.checked, false, "checkbox is not checked in bubbled event" );
2845+
});
2846+
2847+
// Native click
2848+
cb.checked = true;
2849+
equal( cb.checked, true, "native - checkbox is initially checked" );
2850+
cb.click();
2851+
equal( cb.checked, false, "native - checkbox is no longer checked" );
2852+
2853+
// jQuery click
2854+
cb.checked = true;
2855+
equal( cb.checked, true, "jQuery - checkbox is initially checked" );
2856+
jQuery( cb ).click();
2857+
equal( cb.checked, false, "jQuery - checkbox is no longer checked" );
2858+
2859+
// Handlers only; checkbox state remains false
2860+
jQuery( cb ).triggerHandler( "click" );
2861+
});
2862+
28342863
test("fixHooks extensions", function() {
28352864
expect( 2 );
28362865

test/unit/manipulation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ var testWrap = function(val) {
100100
ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
101101
jQuery(checkbox).wrap(val( "<div id='c1' style='display:none;'></div>" ));
102102
ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
103-
}).click();
103+
}).prop( "checked", false )[0].click();
104104

105105
// using contents will get comments regular, text, and comment nodes
106106
var j = jQuery("#nonnodes").contents();

0 commit comments

Comments
 (0)