2
votes

I had declared 5 checkboxes with name and id attribute in a html:

<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/>

After declaration, I want to run a javascript that would enable the checkbox1 using that checkbox id.

Let me know to clarify something.

4
enable how? are they initially disabled?amosrivera
<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
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
I agree but I want to enable the checkbox after their declaration through javascript.Ankit
@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

4 Answers

5
votes

Try 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;
2
votes
document.getElementById('checkbox1').checked = true;
1
votes

try

   document.myform.box1.checked = true;

or

document.getElementById('myid').checked = true;

see an full example : http://www.rgagnon.com/jsdetails/js-0007.html

0
votes

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