Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have 9 checkboxes whose id will be from 1-9. I have an array which consists of numbers like var arr = [1,3,4,5,6,7]. now the checkboxes should be unchecked for those which are not present in the array (in arr array numbers 2,8,9 are not present). so for numbers 2,8,9 the chechboxes should be unchecked and for the numbers in the array should be checked.
chechbox 1: checked

chechbox 3: checked

chechbox 4: checked

chechbox 5: checked

chechbox 6: checked

chechbox 7: checked


What I have tried:

JavaScript
for(var x=0;x<9;x++)
{
if(x==arr[x]
{
document.getElementById(x).checked=true;
}
}
Posted
Updated 5-Apr-17 3:37am
Comments
Wessel Beulink 5-Apr-17 9:26am    
What framework are you using? In mvc c# you should fix this in the controller. For php you have function witch already do this for you.

1 solution

try
var arr = [1, 3, 4, 5, 6, 7];
        for (var x = 1; x <= 9; x++) { 
            document.getElementById(x).checked = arr.indexOf(x) > -1;
        }

refer JavaScript Array indexOf() Method[^]

Demo: - JSFiddle[^]
 
Share this answer
 
v2
Comments
Wessel Beulink 5-Apr-17 9:41am    
Why should you use javaScript to handle checkboxes?
Karthik_Mahalingam 5-Apr-17 9:45am    
since the OP has posted this question under the tag "Javascript"
Wessel Beulink 5-Apr-17 9:55am    
fair enough.
Karthik_Mahalingam 5-Apr-17 9:58am    
:)
R!sh! 5-Apr-17 23:52pm    
I have written everything in javascript.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900