Attie

Attie

  • NA
  • 95
  • 2.3k

Check/UncheckAll Chechbox

Mar 8 2018 3:59 AM
Good Day
 
I have checkboxes that when I click they get checked and then the value be populated into the database (SQL Server), then when I uncheck that textbox the value get removed from the database.
Now what I need is a check/uncheck All checkbox that will do the same. When checking all the values should show into the database, and when unchecking all values should be removed. My code below;
 
HTML
  1. <b>  
  2. <label>  
  3. Check/Uncheck All  
  4. </label>  
  5. <label class="switch">  
  6. <input type="checkbox" id="checkall">  
  7. <span class="slider round">  
  8. </span>  
  9. </label>  
  10. </b>  
  11. <br />  
  12. <br />  
  13. <div id="checkboxes" style="height: 500px;position:relative;border:1px solid darkgray" class='checkbox' >  
  14. <div id="div2" style="max-height:100%;overflow:auto;" class='checkbox' >  
  15. <div id="suburbsDiv" class='checkbox' ></div>  
  16. </div>  
  17. </div>  
JAVASCRIPT FOR CHECK/UNCHECK ALL CHECKBOX
  1. <script type="text/javascript">  
  2. function toggleChecked(status) {  
  3. $("#checkboxes input").each(function () {  
  4. $(this).prop("checked", status);  
  5. });  
  6. }  
  7. $(document).ready(function () {  
  8. // Changing state of CheckAll checkbox  
  9. $("#checkboxes").click(function () {  
  10. if ($("#checkboxes").length == $("#checkboxes input:checked").length)  
  11. {  
  12. $("#checkall").prop("checked"true);  
  13. }  
  14. else  
  15. {  
  16. $("#checkall").removeAttr("checked");  
  17. }  
  18. });  
  19. $("#checkall").click(function () {  
  20. var status = $("#checkall").prop('checked');  
  21. toggleChecked(status);  
  22. });  
  23. });  
  24. </script>

Answers (2)