Created
September 11, 2011 02:40
-
-
Save mmonteleone/1209097 to your computer and use it in GitHub Desktop.
Cloned Radio Input Checked State Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html> | |
<head> | |
<title>Radio Clone Test</title> | |
<script type="text/javascript"> | |
/* Example of checked state of a radio input not being retained in a twice-cloned node. | |
Fails in Safari 4.0.5. | |
Passes in 5.1 */ | |
window.onload = function() { | |
var div = document.createElement("div"); | |
div.innerHTML = "<input type='radio' name='radiotest' checked='checked'/>"; | |
var fragment = document.createDocumentFragment(); | |
fragment.appendChild(div.firstChild); | |
var fragCloneClone = fragment.cloneNode(true).cloneNode(true).lastChild; | |
alert('Cloned check state retained (expected true): ' + fragCloneClone.checked); | |
document.getElementById('formtest').appendChild(fragCloneClone); | |
} | |
</script> | |
</head> | |
<body> | |
<form id="formtest"></form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No, look closely at my example using
defaultChecked
; the "checked" attribute is indeed copied, resulting indefaultChecked == true
. Also try:Results "checked".
So the cloning is working fine. The checkbox is cloned, the checked attribute is cloned.
The state of the object is being lost. That's not the same thing as the attribute being lost.
But why does that happen? Is that a bug? When I remove the element's
name
it retains the state;input.checked
is true and it can be seen in the document that it is ticked off.alerts "true" and the radio looks checked. Also, this does not affect checkboxes; only radios.