0
votes

I am having some trouble with my nested for loop. I am trying to determine the value of checked checkboxes in my form. I was told that the whole thing should formatted like so:

for(checkboxes loop){
   for(item.appointmentType[1] loop){
      if(statement conditional){
          set checks code
      }
   }
}

But I just can not seem to get it written correctly because I have received an error saying, Uncaught TypeError: Object has no method 'setAttribute'

Below is the code that I am working with. Can someone help me correct it that may know what I am talking about?

var checkboxes = document.forms[0].appointmentType;
for(var i=0; i<checkboxes.length; i++){
   for(var j=0; j<item.appointmentType[1].length; j++){
      if(checkboxes[i].value === item.appointmentType[1][j]){
        item.appointmentType[1][j].setAttribute("checked", "checked");
      }
   }
}

Here is the HTML code that goes along with javascript. All of the javascript is contained in it's own .js file.

<ul><span class="lab">
<li><input type="checkbox" checked value="Get A Free Estimate" id="Get A Free Estimate"
    name="appointmentType" /><label for="getAFreeEstimate"><span class="gfe">Get A FREE
    Estimate</span></label></li>
<li><input type="checkbox" value=" Lawn Service" id="lawnService" name="appointmentType"/
    <label for="lawnService"><span class="ls">Lawn Service</span></label></li>
<li><input type="checkbox" value=" Sprinkler System Service" id="sprinklerSystemService"
    name="appointmentType" /><label for="sprinklerSystemService"><span
    class="sss">Sprinkler System Service</span></label></li>
<li><input type="checkbox" value=" Flower Bed Maintenance" id="flowerBedMaintenance"
    name="appointmentType" /><label for="flowerBedMaintenance"><span class="fbm">Flower
    Bed Maintenance</span></label></li>
<li><input type="checkbox" value=" Hedge/Tree Service" id="hedgeTreeService"
    name="appointmentType" /><labe for="hedgeTreeService"><span class="hts">Hedge / Tree
    Service</span></label></li>
<li><input type="checkbox" value=" Handyman Services" id="handymanServices"
    name="appointmentType" /><label for="handymanServices"><span class="hs">Handyman
    Services</span></label></li>
<li><input type="checkbox" value=" Other" id="other" name="appointmentType" /><label
    for="other"><span class="otr">Other</span></label></li></span></ul><br/>
2
How is "item" supposed to get a value? - Pointy
Could you show the HTML that goes with this code? - Barmar

2 Answers

0
votes

This line:

item.appointmentType[1][j].setAttribute("checked", "checked");

Should probably be:

checkboxes[i].checked = true;
0
votes

If you have Jquery rendered in your proyect in think you can replace your line with:

checkboxes = $('input[type="checkbox"]');

$(checkboxes[i]).prop('checked',true);

Maybe would help