Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  115] [ 2]  / answers: 1 / hits: 49827  / 12 Years ago, tue, august 21, 2012, 12:00:00

right now when it first loads the html page, my checkbox was created in this way:



<input type=checkbox id=CBOX1 name=CBOX1 onclick=onCBOX(this) disabled/>


in a function in on the same html:



boolean checked = true;
document.theForm.elements['CBOX1'].checked = true;


For some reason, the checked box value is not checked when the function is called later on the page. Is it because when i first created the checkbox, i created it without a 'checked' attribute? And then when i assign it a value, the element doesnt seem to include the checked attribute anymore as when i check on the source of the page. its still the same...



<input type=checkbox id=CBOX1 name=CBOX1 onclick=onCBOX(this) disabled/>


For simplicity sake, i know for sure that there were changes made to other attributes of this element using AJAX, but i am at a loss to WHY the checked attribute is not carried over... What's the alternative?


More From » html

 Answers
79

Check the checkbox:



document.theForm.elements['CBOX1'].checked = true;
document.theForm.elements['CBOX1'].checked = checked;


Uncheck the checkbox:



document.theForm.elements['CBOX1'].checked = false; 


If you want it unchecked on load then don't touch it, by default it is unchecked.



Moreover, no click events will be registered if you use the disabled attribute on your input field.



See this jsfiddle for an example.



EDIT



If clickability is not the issue then just do what I already pointed out. Here is a full working example.



<html>
<head>
</head>
<body>
<input id=tar type=checkbox disabled />
<input type=checkbox onclick=callFunc(this) />
<script type=text/javascript>
function callFunc(elem){
document.getElementById(tar).checked = elem.checked;
}
</script>
</body>
</html>

[#83502] Monday, August 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;