Skip to content

Commit

Permalink
Fix #12127, fer real. IE9/10 check state on clone. Close gh-875.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchatfield authored and dmethvin committed Jul 26, 2012
1 parent a475f1a commit 155855b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ David Benjamin <davidben@mit.edu>
Uri Gilad <antishok@gmail.com> Uri Gilad <antishok@gmail.com>
Chris Faulkner <thefaulkner@gmail.com> Chris Faulkner <thefaulkner@gmail.com>
Elijah Manor <elijah.manor@gmail.com> Elijah Manor <elijah.manor@gmail.com>
Daniel Chatfield <chatfielddaniel@googlemail.com>
5 changes: 2 additions & 3 deletions src/manipulation.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -455,9 +455,8 @@ function cloneFixAttributes( src, dest ) {
// IE6-8 fails to persist the checked state of a cloned checkbox // IE6-8 fails to persist the checked state of a cloned checkbox
// or radio button. Worse, IE6-7 fail to give the cloned element // or radio button. Worse, IE6-7 fail to give the cloned element
// a checked appearance if the defaultChecked value isn't also set // a checked appearance if the defaultChecked value isn't also set
if ( src.checked ) {
dest.defaultChecked = dest.checked = src.checked; dest.defaultChecked = dest.checked = src.checked;
}


// IE6-7 get confused and end up setting the value of a cloned // IE6-7 get confused and end up setting the value of a cloned
// checkbox/radio button to an empty string instead of "on" // checkbox/radio button to an empty string instead of "on"
Expand Down
12 changes: 12 additions & 0 deletions test/unit/manipulation.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1895,3 +1895,15 @@ test("html() - script exceptions bubble (#11743)", function() {
ok( false, "error ignored" ); ok( false, "error ignored" );
}, "exception bubbled from remote script" ); }, "exception bubbled from remote script" );
}); });

test("checked state is cloned with clone()", function(){
expect(2);

var elem = jQuery.parseHTML("<input type='checkbox' checked='checked'/>")[0];
elem.checked = false;
equal( jQuery(elem).clone().attr("id","clone")[0].checked, false, "Checked false state correctly cloned" );

elem = jQuery.parseHTML("<input type='checkbox'/>")[0];
elem.checked = true;
equal( jQuery(elem).clone().attr("id","clone")[0].checked, true, "Checked true state correctly cloned" );
});

2 comments on commit 155855b

@programmin1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is apparently broken in the jQuery v1.12.4 that comes with Wordpress.
Run this at the console and you will get true, not false:

var elem = jQuery.parseHTML("<input type='checkbox' checked='checked'/>")[0];
elem.checked = false;
console.log( jQuery(elem).clone()[0].checked);

@mgol
Copy link
Member

@mgol mgol commented on 155855b Dec 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@programmin1 This works fine for me in IE 10: http://jsbin.com/dimowe/edit?html,js,console

If you can tweak this jsbin until it shows the issue you have, please open a new issue with the link to this jsbin.

Please sign in to comment.