title: How to add a background color to the td element when you check the checkbox in it
body: <p>I am trying to iterate through the checkboxes and give a green background color to the checkboxes which were ticked.</p>
  <p>I have the main checkbox which disables the rest of the checkboxes so the disabled checkboxes should not have a green background color as they are disabled.</p>
  <p>Also I will have more rows in the table and I wonder how am I suppose to avoid repeat the same block of code on every row with tons of new classes.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let carParkCheckMain = document.querySelector('.car-park-check-main');
  let carParkChecks = document.querySelectorAll('.car-park-check');
  let tdAll = document.querySelectorAll('.access-td');
  
  // Uncheck all checkboxes
  for (let i = 0; i &lt; carParkChecks.length; i++) {
    disable(carParkChecks[i]);
  }
  
  // Run through
  for (let td of tdAll) {
    td.addEventListener('mouseenter', () =&gt; {
      td.addEventListener('click', () =&gt; {
        if (carParkCheckMain.checked) {
          for (let i = 0; i &lt; carParkChecks.length; i++) {
            enable(carParkChecks[i]);
          }
        } else {
          for (let i = 0; i &lt; carParkChecks.length; i++) {
            disable(carParkChecks[i]);
          }
        }
      })
    })
  }
  
  
  function disable(checkbox) {
    return checkbox.disabled = true;
  }
  
  function enable(checkbox) {
    return checkbox.disabled = false;
  }
  
  function greenBg(element) {
    element.classList.toggle('td-active');
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.td-active {
    background-color: rgb(95, 241, 95) !important;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table class="table table-bordered access-table"&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;th scope="row" style="font-weight: 300;"&gt;Checkboxes&lt;/th&gt;
        &lt;td class="main-td access-td one"&gt;
          &lt;label&gt;
                                          &lt;input type="checkbox" class="access-table-input car-park-check-main"&gt; main checkbox
                                      &lt;/label&gt;
        &lt;/td&gt;
        &lt;td class="base-td access-td two"&gt;
          &lt;label&gt;
                                          &lt;input type="checkbox" class="access-table-input car-park-check" disabled&gt; secondary checkbox
                                      &lt;/label&gt;
        &lt;/td&gt;
        &lt;td class="base-td access-td four"&gt;
          &lt;label&gt;
                                          &lt;input type="checkbox" class="access-table-input car-park-check" disabled&gt; secondary checkbox
                                      &lt;/label&gt;
        &lt;/td&gt;
        &lt;td class="base-td access-td five"&gt;
          &lt;label&gt;
                                          &lt;input type="checkbox" class="access-table-input car-park-check" disabled&gt; secondary checkbox
                                      &lt;/label&gt;
        &lt;/td&gt;
        &lt;td class="base-td access-td six"&gt;
          &lt;label&gt;
                                          &lt;input type="checkbox" class="access-table-input car-park-check" disabled&gt; secondary checkbox
                                      &lt;/label&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>