2

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.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Ankit
  • 2,126
  • 4
  • 33
  • 53
  • enable how? are they initially disabled? – amosrivera Jan 19 '11 at 08:25
  • One
    Two
    Three
    Four
    – Ankit Jan 19 '11 at 08: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 Jan 19 '11 at 08:29
  • I agree but I want to enable the checkbox after their declaration through javascript. – Ankit Jan 19 '11 at 08: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 Jan 19 '11 at 08:34
  • Possible duplicate of [Check/Uncheck checkbox with JavaScript?](https://stackoverflow.com/questions/8206565/check-uncheck-checkbox-with-javascript) – user202729 Jan 16 '19 at 05:34

4 Answers4

5

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;
KishoreK
  • 892
  • 5
  • 14
1

try

   document.myform.box1.checked = true;

or

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

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

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
1
document.getElementById('checkbox1').checked = true;
jAndy
  • 231,737
  • 57
  • 305
  • 359
0

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

Prateek Gupta
  • 2,422
  • 2
  • 16
  • 30