3

I am using a Jquery Accordion. I have inside each div 10 or more checkboxes. I am using Jquery to toggle the class and change the BG-color. I am also saving the status of each checkbox into a database. When the page loads again, it does show the correct status for each checkbox, but does not change the class, I believe because this happens on change. Also, I need to be able to change the header color if all the checkboxes in a div are checked. The Jquery I am using right now is

$('input[type=checkbox]').on('change', function () {
$(this).closest('fieldset')
    .removeClass('bg-info', this.checked) // remove the bg-info once clicked
    .toggleClass('bg-warning', !this.checked) // show warning when un-checked
    .toggleClass('bg-success', this.checked); // show success when checked

});

and the html is like

<div id="Accordion1">
<h3 class = "col-lg-12 text-center bg-warning "><a href="#">Step 1 </a></h3>

<div id= "acc1">
   <form role="form" action="pdi-run.php" method="post" name='details'>

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="1" onClick="checkval(this)" <?php echo $boxes[1] ? 'checked = "checked"' : '' ?>>Inspected 
     </fieldset>  

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="2" onClick="checkval(this)"<?php echo $boxes[2] ? 'checked = "checked"' : '' ?>>All 
     </fieldset> 

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="3" onClick="checkval(this)"<?php echo $boxes[3] ? 'checked = "checked"' : '' ?>>All 
     </fieldset>

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="4" onClick="checkval(this)"<?php echo $boxes[4] ? 'checked = "checked"' : '' ?>>Trunk 
     </fieldset> 

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="5" onClick="checkval(this)"<?php echo $boxes[5] ? 'checked = "checked"' : '' ?>>aligns 
     </fieldset>

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="6" onClick="checkval(this)"<?php echo $boxes[6] ? 'checked = "checked"' : '' ?>>present
     </fieldset> 

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="7" onClick="checkval(this)"<?php echo $boxes[7] ? 'checked = "checked"' : '' ?>>cracks
     </fieldset> 

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="8" onClick="checkval(this)"<?php echo $boxes[8] ? 'checked = "checked"' : '' ?>>Front 
     </fieldset> 

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="9" onClick="checkval(this)"<?php echo $boxes[9] ? 'checked = "checked"' : '' ?>>Weather
     </fieldset> 

     <fieldset class="form-horizontal form-group bg-info">  
        <input type="checkbox" class="box " name="checkBox[]" id="10"  onClick="checkval(this)"<?php echo $boxes[10] ? 'checked = "checked"' : '' ?>>Key 
     </fieldset> 

     <textarea class= "form-control notes" name="notes[]" data-num="1" onChange="getVal(this)" placeholder = "If any of the above items were etc."><?php echo $notes[1] ? $notes[1] : '' ?></textarea>

</div>
2
  • You cannot do this with just jquery and css, if you're saving these in a database, then you will need to get this data out again using PHP, for example, and then apply a check and add a class that way. Commented May 12, 2016 at 20:29
  • @Karl I think you misunderstand; "I am also saving the status of each checkbox into a database. When the page loads again, it does show the correct status for each checkbox, but does not change the class". He's not confused about HTML statelessness. Commented May 12, 2016 at 20:34

1 Answer 1

2

I think this sounds simple enough. You just want to "refresh" the class state of the fieldset on init, not just when changed.

function refreshCheckboxes() {
$(this).closest('fieldset')
    // Your second parameter, I believe, wasn't actually doing anything
    .removeClass('bg-info');
    .toggleClass('bg-warning', !this.checked) // show warning when un-checked
    .toggleClass('bg-success', this.checked); // show success when checked
}
$('input[type=checkbox]').on('change', refreshCheckboxes).each(refreshCheckboxes);
Sign up to request clarification or add additional context in comments.

1 Comment

this pretty much worked. Now everything that is not checked shows as warning, instead of info, but I am just gonna drop the warning part and have it either be info or success. thx. I will write the second part of this question up as a different question if I cannot get it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.