Skip to content
Permalink
Browse files

Fix #3827. Get the correct checkbox status for a click handler.

  • Loading branch information...
dmethvin committed Nov 21, 2012
1 parent 4fed8eb commit 1fb2f92c357b985a5ba18d0938edd27b5f64d8c3
Showing with 40 additions and 3 deletions.
  1. +10 −2 src/event.js
  2. +29 −0 test/unit/event.js
  3. +1 −1 test/unit/manipulation.js
@@ -263,7 +263,7 @@ jQuery.event = {

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

@@ -523,7 +523,15 @@ jQuery.event = {
// Prevent triggered image.load events from bubbling to window.load
noBubble: true
},

click: {
// For checkbox, fire native event so checked state will be right
trigger: function() {
if ( jQuery.nodeName( this, "input") && this.type === "checkbox" && this.click ) {
this.click();
return false;
}
}
},
focus: {
delegateType: "focusin"
},
@@ -2831,6 +2831,35 @@ test("clone() delegated events (#11076)", function() {
clone.remove();
});

test("checkbox state (#3827)", function() {
expect( 9 );

var markup = jQuery("<div><input type=checkbox><div>").appendTo("#qunit-fixture"),
cb = markup.find("input")[0];

jQuery(cb).on( "click", function(){
equal( this.checked, false, "just-clicked checkbox is not checked" );
});
markup.on( "click", function(){
equal( cb.checked, false, "checkbox is not checked in bubbled event" );
});

// Native click
cb.checked = true;
equal( cb.checked, true, "native - checkbox is initially checked" );
cb.click();
equal( cb.checked, false, "native - checkbox is no longer checked" );

// jQuery click
cb.checked = true;
equal( cb.checked, true, "jQuery - checkbox is initially checked" );
jQuery( cb ).click();
equal( cb.checked, false, "jQuery - checkbox is no longer checked" );

// Handlers only; checkbox state remains false
jQuery( cb ).triggerHandler( "click" );
});

test("fixHooks extensions", function() {
expect( 2 );

@@ -100,7 +100,7 @@ var testWrap = function(val) {
ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
jQuery(checkbox).wrap(val( "<div id='c1' style='display:none;'></div>" ));
ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
}).click();
}).prop( "checked", false )[0].click();

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

0 comments on commit 1fb2f92

Please sign in to comment.
You can’t perform that action at this time.