Bug Tracker

Opened 14 years ago

Closed 12 years ago

#3879 closed bug (fixed)

appending or cloning a checked checkbox or radio button doesn't maintain the default state

Reported by: Pyro979 Owned by: snover
Priority: blocker Milestone: 1.5
Component: manipulation Version: 1.4.4
Keywords: append checked selected defaultValue defaultSelected defaultChecked Cc:
Blocked by: Blocking:

Description

This looks like a similar bug to #769, but I am experiencing this in IE7.

var check=$(document.createElement("input")).attr("type","checkbox"); check.attr("checked",true);

$("#id_main1").append(check);

The appended checkbox is no longer checked. There is an obvious workaround I can (and did) write meanwhile. But this is a pretty serious bug in my opinion.

Attachments (1)

test.html (485 bytes) - added by Pyro979 14 years ago.
test case

Download all attachments as: .zip

Change History (34)

comment:1 Changed 14 years ago by rightwildman

check.attr("checked", true) should not work at all because it is check.attr("checked", "checked") see W3C XHTML

Changed 14 years ago by Pyro979

Attachment: test.html added

test case

comment:2 Changed 14 years ago by Pyro979

Updated the test case, bug still applies.

comment:3 Changed 14 years ago by john

Milestone: 1.3.1
Owner: set to john

Is this a bug that's new in jQuery 1.3 or did it also exist in jQuery 1.2.6?

comment:4 Changed 14 years ago by Pyro979

It exists in 1.2.6 as well.

comment:5 Changed 14 years ago by dmethvin

This is an old bug with IE6/7 appendChild, it clears the checked state of a checkbox. I thought there was a ticket related to the same issue with .clone() but I can't find it.

comment:6 Changed 13 years ago by dmethvin

See also #5758, which shows a similar problem with radios.

comment:7 Changed 13 years ago by icfantv

We're running in to this issue as well. Anyone know the status of fixing this? In the interim, we can set the checked state as part of the HTML construction, i.e. $('<input type="checkbox" checked="checked"/>') as a workaround.

While @Wildman may be correct re the spec, even IE6 supports using boolean flags with the .attr(...) function. That is to say, once the checkbox is added to the DOM, <obj>.attr('checked', true) works as expected. However, as noted in the bug, prior to adding to the DOM, the attr(...) function does not work - even when chained at the end of the append() function.

comment:8 Changed 13 years ago by dmethvin

Per #6377, also happens with radio elements.

comment:9 Changed 13 years ago by dmethvin

Component: coremanipulation

comment:10 Changed 13 years ago by boushley

Created a jsfiddle testcase http://jsfiddle.net/boushley/R3Wxx/ is running against 1.3.2 and http://jsfiddle.net/boushley/R3Wxx/1/ is running against 0 GIT. I can reproduce it from IE 7.

comment:11 Changed 13 years ago by Rick Waldron

Status: newopen

Tested and passing in:

  • FF3.0.12,3.6.11,4b6
  • Chrome 5,6,7,8
  • Safari 5
  • IE 6,7,8
  • Opera 10.60

http://jsfiddle.net/rwaldron/nypNd/5/

comment:12 Changed 13 years ago by Rick Waldron

Resolution: worksforme
Status: openclosed

Per addyosmani request, a third method:

http://jsfiddle.net/rwaldron/nypNd/7/

comment:13 Changed 13 years ago by Rick Waldron

comment:14 Changed 13 years ago by Rick Waldron

Milestone: 1.3.1
Resolution: worksforme
Status: closedreopened

comment:15 Changed 13 years ago by Rick Waldron

Resolution: wontfix
Status: reopenedclosed

To clarify, my cases are meant to show proven, tested workaround to this unfixable, unsolvable problem.

comment:16 Changed 13 years ago by Rick Waldron

Resolution: wontfix
Status: closedreopened

comment:17 Changed 13 years ago by snover

#5758 is a duplicate of this ticket.

comment:18 Changed 13 years ago by snover

Summary: appending a checked checkbox doesn't maintain the cheked stateappending a checked checkbox or radio button doesn't maintain the checked state

comment:19 Changed 13 years ago by snover

#6486 is a duplicate of this ticket.

comment:20 Changed 13 years ago by snover

#3704 is a duplicate of this ticket.

comment:21 Changed 13 years ago by snover

Keywords: checked selected defaultValue defaultSelected defaultChecked added; checkbox removed
Summary: appending a checked checkbox or radio button doesn't maintain the checked stateappending or cloning a checked checkbox or radio button doesn't maintain the default state

comment:22 Changed 13 years ago by AP Erebus

I can confirm that this is still an issue in 1.4.2, and assuming that it's still open, in 1.4.3+.

The workaround suggested by icfantv still works.

comment:23 Changed 13 years ago by snover

Priority: majorlow
Resolution: patchwelcome
Status: reopenedclosed
Version: 1.31.4.4

I don’t know if there is really anything that can be done about this in a way that is satisfactory; if someone wants to try their hand at a patch, we would be happy to review it.

comment:24 Changed 13 years ago by julien

I wish this ticket weren't closed. I've actually noticed the exact same issue in Chrome.

comment:25 in reply to:  24 Changed 13 years ago by Rick Waldron

Replying to julien:

I wish this ticket weren't closed. I've actually noticed the exact same issue in Chrome.

As snover mentioned, patches are welcome.

comment:26 Changed 12 years ago by fletch@…

Didn't see a test case using clone: http://www.jsfiddle.net/fletch/CLq3P/

Works fine in Firefox 3.6 and Chrome 8, does not work in IE8.

comment:27 Changed 12 years ago by snover

Milestone: 1.5
Resolution: patchwelcome
Status: closedreopened

comment:28 Changed 12 years ago by snover

Owner: changed from john to snover
Status: reopenedassigned

comment:29 Changed 12 years ago by Pyro979

Let me say right away that I'm not sure that I'm doing this correctly, and whether or not this is the "proper" way to fix this bug. But this seems to work. I can probably benefit from some more optimization

https://github.com/Pyro979/jquery

comment:30 Changed 12 years ago by Pyro979

Just added a jQuery.support.checkboxAppendPermanence check into support.js. This should improve performance since we won't need to do any of this "checked" re-assigning when it works fine. Don't know if I put it into the right spot.

comment:31 Changed 12 years ago by snover

Pyro979, there is already an outstanding open pull request for this issue; you don’t need to write code to fix this ticket.

comment:32 Changed 12 years ago by snover

Priority: lowblocker

comment:33 Changed 12 years ago by Colin Snover

Resolution: fixed
Status: assignedclosed

Ensure that buildFragment clones elements properly in all browsers. Fixes #3879, #6655. Also improves form element clone tests and fixes bugs in $.fn.clone exposed by these new test cases related to the values of checkboxes and radio buttons in IE.

Changeset: b14f02899e74c429effadd88527ffde17650149a

Note: See TracTickets for help on using tickets.