script.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const selectAllCheckboxes = document.querySelector("#select-all");
  2. const checkboxes = document.querySelectorAll(".checkbox");
  3. const deleteBtn = document.querySelector("#delete");
  4. const redriveBtn = document.querySelector("#redrive");
  5. selectAllCheckboxes.addEventListener("click", eventCheckBox);
  6. /**
  7. * @description Event handler for select all checkbox
  8. */
  9. function eventCheckBox() {
  10. if (selectAllCheckboxes.checked) {
  11. checkboxes.forEach((checkbox) => {
  12. checkbox.checked = true;
  13. });
  14. } else {
  15. checkboxes.forEach((checkbox) => {
  16. checkbox.checked = false;
  17. });
  18. }
  19. }
  20. /**
  21. * @description Event handler for checkbox to enable/disable delete button
  22. */
  23. function handleCheckboxChange() {
  24. var atLeastOneChecked = false;
  25. checkboxes.forEach(function (checkbox) {
  26. if (checkbox.checked) {
  27. atLeastOneChecked = true;
  28. }
  29. });
  30. deleteBtn.disabled = !atLeastOneChecked;
  31. redriveBtn.disabled = !atLeastOneChecked;
  32. }
  33. checkboxes.forEach((checkbox) => {
  34. checkbox.addEventListener("change", handleCheckboxChange);
  35. });
  36. selectAllCheckboxes.addEventListener("click", handleCheckboxChange);