I have some checkboxes like this in my form...
<label><input type="checkbox" name="project_dept[]" id="project_dept1" value="1">Development</label>
<label> <input type="checkbox" name="project_dept[]" id="project_dept2" value="2">Designing</label>
<label> <input type="checkbox" name="project_dept[]" id="project_dept3" value="3">Testing</label>
<label> <input type="checkbox" name="project_dept[]" id="project_dept4" value="4">Finance</label>
Now, in javascript I have a string in a format of comma separated values like,
var projDepts = "1,2,3,4";
after using the split function of javascript i have converted it to an array.
Now, my question is I want to select all those checkboxes as per the values in the array. I have tried something but its not working for me,
var projDepts = "1,2,3,4";
var projDeptArray = projDepts.split(',');
var project_depts = document.getElementsByName("prdept[]");
for (var i = 0; i < projDeptArray.length; i++) {
for (var j = 0; j < project_depts.length; j++) {
if (projDeptArray[i] == project_depts[j]) {
$(project_depts[i]).prop("checked", true)
}
}
}
This code is not working and none of the checkboxes got checked, I know I am getting somewhere wrong. Please someone help me to a solution for this.
prdept[]!=project_dept[]...