title: Color table's row on click using Javascript and CSS
body: <p>I have a table and I would like to color a row on click using Javascript.</p>
  <p>Here is my code, there are three rows in the table, two functions in javascript and one CSS file :</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function changeColor() {
    var td = $("#myTable" + " td");
    $.each(td, function(i) {
  
      if ($(td[i]).html() === 'Cloture') {
        $(td[i]).addClass("green");
      } else if ($(td[i]).html() === 'A faire') {
        $(td[i]).addClass("red");
      } else if ($(td[i]).html() === 'En cours') {
        $(td[i]).addClass("orange");
      }
      else {
        $(td[i]).addClass("white");
      }
    });
  }
  
  changeColor();
  
  function selectRow() {
    var table = document.getElementById("myTable");
    [...table.rows].forEach(el =&gt; {
      el.addEventListener('click', () =&gt; {
        el.classList.toggle('selected-row');
      });
    });
  }
  
  selectRow();</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code> td{
              text-align: center;
              color: white;
              background: #FFFFFF;
              padding: 10px;
          }
  
          tr:hover {background-color: #FFE56C;}
  
          td:last-child {
            border-right: 1px;
          }
          .green {
            background-color: #239e58 !important;
          }
          .red {
            background-color: #ff4e33 !important;
          }
          .orange {
            background-color: #ffd633 !important;
          }
  
          .white {
            background-color: white !important;
          }
  
          .table-row {
              cursor: pointer;
              }
  
          .selected-row {
            background-color: #FFD200 !important;
            color: white;
          }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt;
  
      &lt;title&gt;Actions EAVP&lt;/title&gt;
      &lt;link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"&gt;
      &lt;script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/datepicker/1.0.10/datepicker.min.js" integrity="sha512-RCgrAvvoLpP7KVgTkTctrUdv7C6t7Un3p1iaoPr1++3pybCyCsCZZN7QEHMZTcJTmcJ7jzexTO+eFpHk4OCFAg==" crossorigin="anonymous" referrerpolicy="no-referrer"&gt;&lt;/script&gt;
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css"&gt;
      &lt;link rel="stylesheet" type="text/css" href="/static/polls/style.css" /&gt;
  
  &lt;div class="container"&gt;
  
      &lt;body style="background-color: white";&gt;
  
          &lt;section class="content"&gt;
              
  &lt;div&gt;
  &lt;/div&gt;
      &lt;br&gt;
  &lt;h3 style="color: black;"&gt;Liste des actions&lt;/h3&gt;
  
  &lt;div style="overflow-x:auto;"&gt;
  
      &lt;table class="list_homepage" id="myTable"&gt;
          &lt;th style="color: white; background-color:  #03234B; padding: 5px; border: 1px solid white; text-align: center"&gt;&lt;/th&gt;
          &lt;th style="color: white; background-color:  #03234B; padding: 5px; border: 1px solid white; text-align: center"&gt;id&lt;/th&gt;
          &lt;th style="color: white; background-color:  #03234B; padding: 5px; border: 1px solid white; text-align: center"&gt;titre&lt;/th&gt;
          &lt;th style="color: white; background-color:  #03234B; padding: 5px; border: 1px solid white; text-align: center"&gt;date_rendu&lt;/th&gt;
          &lt;th style="color: white; background-color:  #03234B; padding: 5px; border: 1px solid white; text-align: center"&gt;statut&lt;/th&gt;
          &lt;th style="color: white; background-color:  #03234B; padding: 5px; border: 1px solid white; text-align: center"&gt;date_insertion&lt;/th&gt;
          &lt;th style="color: white; background-color:  #03234B; padding: 5px; border: 1px solid white; text-align: center"&gt;zone_emettrice&lt;/th&gt;
          &lt;th style="color: white; background-color:  #03234B; padding: 5px; border: 1px solid white; text-align: center"&gt;zone_receptrice&lt;/th&gt;
          &lt;th style="color: white; background-color:  #03234B; padding: 5px; border: 1px solid white; text-align: center"&gt;owner&lt;/th&gt;
          
  
          
          &lt;tr class="table-row"&gt;
          &lt;td style="border-bottom: 1px solid black;"&gt;
              &lt;a style="color: #ff0000; text-align: center;" href="/action/1/delete/"&gt;&lt;i class="bi bi-trash3" style="font-size: 30px;"&gt;&lt;/i&gt;&lt;/a&gt;
              &lt;a style="color: #3CB4E6; text-align: center;" href="/action/1/update/"&gt;&lt;i class="bi bi-pencil-square" style="font-size: 30px;"&gt;&lt;/i&gt;&lt;/a&gt;
          &lt;br&gt;
              &lt;a style="; text-align: center;" href="/action/1/"&gt;Detail&lt;/a&gt;&lt;/td&gt;
  
              
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;1&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;2022-05-20 22:00:00+00:00&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;A faire&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;2022-05-06 13:17:07+00:00&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
  
              
          &lt;/tr&gt;
  
          
          &lt;tr class="table-row"&gt;
          &lt;td style="border-bottom: 1px solid black;"&gt;
              &lt;a style="color: #ff0000; text-align: center;" href="/action/2/delete/"&gt;&lt;i class="bi bi-trash3" style="font-size: 30px;"&gt;&lt;/i&gt;&lt;/a&gt;
              &lt;a style="color: #3CB4E6; text-align: center;" href="/action/2/update/"&gt;&lt;i class="bi bi-pencil-square" style="font-size: 30px;"&gt;&lt;/i&gt;&lt;/a&gt;
          &lt;br&gt;
              &lt;a style="; text-align: center;" href="/action/2/"&gt;Detail&lt;/a&gt;&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;2&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;2022-05-24 22:00:00+00:00&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Cloture&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;2022-05-06 13:18:34+00:00&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
  
              
          &lt;/tr&gt;
  
          
          &lt;tr class="table-row"&gt;
          &lt;td style="border-bottom: 1px solid black;"&gt;
              &lt;a style="color: #ff0000; text-align: center;" href="/action/5/delete/"&gt;&lt;i class="bi bi-trash3" style="font-size: 30px;"&gt;&lt;/i&gt;&lt;/a&gt;
              &lt;a style="color: #3CB4E6; text-align: center;" href="/action/5/update/"&gt;&lt;i class="bi bi-pencil-square" style="font-size: 30px;"&gt;&lt;/i&gt;&lt;/a&gt;
          &lt;br&gt;
              &lt;a style="; text-align: center;" href="/action/5/"&gt;Detail&lt;/a&gt;&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;5&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;2022-05-23 22:00:00+00:00&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;En cours&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;2022-05-06 14:24:29+00:00&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
              &lt;td style="color: black; text-align: center; border-bottom: 1px solid black;"&gt;Data&lt;/td&gt;
  
              
          &lt;/tr&gt;
      &lt;/table&gt;
  
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>As you can see I have another javascript function that works well, but the function to color rows on click doesn't work.</p>
  <p>If someone has a solution to my problem that would be great !</p>