Bug Tracker

Ticket #3879 (closed bug: fixed)

Opened 6 years ago

Last modified 4 years ago

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:
Blocking: Blocked by:

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

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

Change History

comment:1 Changed 6 years ago by rightwildman

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

Changed 6 years ago by Pyro979

test case

comment:2 Changed 6 years ago by Pyro979

Updated the test case, bug still applies.

comment:3 Changed 6 years ago by john

  • Owner set to john
  • Milestone set to 1.3.1

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

comment:4 Changed 6 years ago by Pyro979

It exists in 1.2.6 as well.

comment:5 Changed 6 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 5 years ago by dmethvin

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

comment:7 Changed 4 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 4 years ago by dmethvin

Per #6377, also happens with radio elements.

comment:9 Changed 4 years ago by dmethvin

  • Component changed from core to manipulation

comment:10 Changed 4 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 4 years ago by rwaldron

  • Status changed from new to open

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 4 years ago by rwaldron

  • Status changed from open to closed
  • Resolution set to worksforme

Per addyosmani request, a third method:

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

comment:13 Changed 4 years ago by rwaldron

comment:14 Changed 4 years ago by rwaldron

  • Status changed from closed to reopened
  • Resolution worksforme deleted
  • Milestone 1.3.1 deleted

comment:15 Changed 4 years ago by rwaldron

  • Status changed from reopened to closed
  • Resolution set to wontfix

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

comment:16 Changed 4 years ago by rwaldron

  • Status changed from closed to reopened
  • Resolution wontfix deleted

comment:17 Changed 4 years ago by snover

#5758 is a duplicate of this ticket.

comment:18 Changed 4 years ago by snover

  • Summary changed from appending a checked checkbox doesn't maintain the cheked state to appending a checked checkbox or radio button doesn't maintain the checked state

comment:19 Changed 4 years ago by snover

#6486 is a duplicate of this ticket.

comment:20 Changed 4 years ago by snover

#3704 is a duplicate of this ticket.

comment:21 Changed 4 years ago by snover

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

comment:22 Changed 4 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 4 years ago by snover

  • Priority changed from major to low
  • Status changed from reopened to closed
  • Version changed from 1.3 to 1.4.4
  • Resolution set to patchwelcome

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 follow-up: ↓ 25 Changed 4 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 4 years ago by rwaldron

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 4 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 4 years ago by snover

  • Status changed from closed to reopened
  • Resolution patchwelcome deleted
  • Milestone set to 1.5

comment:28 Changed 4 years ago by snover

  • Owner changed from john to snover
  • Status changed from reopened to assigned

comment:29 Changed 4 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 4 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 4 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 4 years ago by snover

  • Priority changed from low to blocker

comment:33 Changed 4 years ago by Colin Snover

  • Status changed from assigned to closed
  • Resolution set to fixed

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.