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?
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?
Share Improve this question edited Aug 21, 2012 at 16:03 bouncingHippo asked Aug 21, 2012 at 15:34 bouncingHippobouncingHippo 6,04023 gold badges71 silver badges110 bronze badges 16-
Is
checked
a variable? If yes, where is it defined? What value does it have? You have to assign a boolean value todocument.theForm.elements['CBOX1'].checked
. – Felix Kling Commented Aug 21, 2012 at 15:36 -
Where does
checked
e from in that first line of JavaScript? Thechecked
property of a checkbox is supposed to be a boolean, true or false, value. – Anthony Grist Commented Aug 21, 2012 at 15:37 -
Is
checked
a defined boolean var that is true or false ? – mplungjan Commented Aug 21, 2012 at 15:37 - Lol. 3 ppl with the same question – mplungjan Commented Aug 21, 2012 at 15:37
- 1 possible duplicate of How do I check a checkbox with jQuery or JavaScript? – Travis J Commented Aug 21, 2012 at 16:44
3 Answers
Reset to default 7Check 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>
Try setting it to true
instead:
document.theForm.elements['CBOX1'].checked = true;
Try this, not tested anyway.
document.theForm.elements['CBOX1'].checked = true;