I have 5 checkboxes with name and id attribute:
<input type="checkbox" name="category" value="One" id=11>One<br/>
<input type="checkbox" name="category" value="Two" id=12>Two<br/>
<input type="checkbox" name="category" value="Three" id=13>Three<br/>
<input type="checkbox" name="category" value="Four" id=14>Four<br/>
I want Javascript code to enable checkbox1 using that checkbox id.
I have 5 checkboxes with name and id attribute:
<input type="checkbox" name="category" value="One" id=11>One<br/>
<input type="checkbox" name="category" value="Two" id=12>Two<br/>
<input type="checkbox" name="category" value="Three" id=13>Three<br/>
<input type="checkbox" name="category" value="Four" id=14>Four<br/>
I want Javascript code to enable checkbox1 using that checkbox id.
Share Improve this question edited Jul 1, 2024 at 8:25 Rob Bednark 28.3k27 gold badges88 silver badges130 bronze badges asked Jan 19, 2011 at 8:21 AnkitAnkit 2,2564 gold badges34 silver badges54 bronze badges 6- enable how? are they initially disabled? – amosrivera Commented Jan 19, 2011 at 8:25
- <input type="checkbox" name="category" value="One" id=11>One<br/> <input type="checkbox" name="category" value="Two" id=12>Two<br/> <input type="checkbox" name="category" value="Three" id=13>Three<br/> <input type="checkbox" name="category" value="Four" id=14>Four<br/> <script type="text/javascript">/*JS Code to enable the Checkbox 1*/ </script> – Ankit Commented Jan 19, 2011 at 8:28
-
If you are generating the code on the server side (or even if it is static code), you know you can make a checkbox be ticked by default by adding
checked="checked"
as attribute? – Felix Kling Commented Jan 19, 2011 at 8:29 - I agree but I want to enable the checkbox after their declaration through javascript. – Ankit Commented Jan 19, 2011 at 8:32
- @ankitjava: So you are creating them with JS. You did not say that ;) (at least not that it was clear to me :D) – Felix Kling Commented Jan 19, 2011 at 8:34
4 Answers
Reset to default 5Try this for checking the checkbox:
document.getElementById(<id of first checkbox>).checked = true;
Try this for enabling the checkbox:
document.getElementById(<id of first checkbox>).disabled = false;
try
document.myform.box1.checked = true;
or
document.getElementById('myid').checked = true;
see an full example : http://www.rgagnon./jsdetails/js-0007.html
document.getElementById('checkbox1').checked = true;
To check a check box you can use :
document.getElementById("Checkbox_ID").checked = true;
And to uncheck a check box you can use :
document.getElementById("Checkbox_ID").checked = false;
And for more info visit this page