Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
Click elsewhere and checkbox is ticked/unticked

So I have a piece of code to create a table with some data in it. The first column contains checkboxes, other columns are just regular data.

What I want is that when I click anywhere on a random row (except for header), that entire row's background is turning into yellow (initially white) and the checkbox in that row is ticked. If I click again, everything in the row back to its normal (white row and unticked checkbox).

When I click right at the checkbox, everything acts as expected. However, when I click elsewhere, only the background is changed. I have checked the checked attribute of the checkbox and it is changed whenever I click (which is like normal). I don't know why this happens. I have looked through many posts with similar issues but it is not helped. I am only a beginner at Javascript and HTML. Please help me with this. Thank you very much!

function changeColorRow(number) {
  var elem = document.getElementsByTagName("tr")[number]
  var curr_check = document.getElementsByClassName("checkbox").checked
  if (!curr_check) {
    elem.style.backgroundColor = "yellow"
    document.getElementsByClassName("checkbox").checked = true
  } else {
    elem.style.backgroundColor = "white"
    document.getElementsByClassName("checkbox").checked = false
  }
}
<h1>List of companies</h1>
<table id="company">
  <tr>
    <th>Choose</th>
    <th>ID</th>
    <th>Company</th>
    <th>Website</th>
  </tr>
  <tr onclick="changeColorRow(1)">
    <td><input type="checkbox" class="checkbox"></td>
    <td>1</td>
    <td>ECOPRO Co., Ltd</td>
    <td>http://www.ecoprovn.com</td>
  </tr>
  <tr onclick="changeColorRow(2)">
    <td><input type="checkbox" class="checkbox"></td>
    <td>2</td>
    <td>WAVINA.COM</td>
    <td>http://www.wavina.com</td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

document.getElementsByClassName returns an array like structure, You have to use index to get that particular element at that index.

document.getElementsByClassName("checkbox")

You can get the index of the clicked row as

index = number - 1

const allCheckbox = document.getElementsByClassName("checkbox");

function changeColorRow(number) {
  const index = number - 1;
  var elem = document.getElementsByTagName("tr")[number]
  var curr_check = document.getElementsByClassName("checkbox")[index].checked

  if (!curr_check) elem.style.backgroundColor = "yellow"
  else elem.style.backgroundColor = "white"

  allCheckbox[index].checked = !allCheckbox[index].checked
}

[...allCheckbox].forEach(cb => {
  cb.addEventListener("change", (e) => changeColorRow(parseInt(e.target.dataset.index)))
})
tr {
  cursor: default;
  user-select: none;
}
<h1>List of companies</h1>
<table id="company">
  <tr>
    <th>Choose</th>
    <th>ID</th>
    <th>Company</th>
    <th>Website</th>
  </tr>
  <tr onclick="changeColorRow(1)">
    <td><input type="checkbox" class="checkbox" data-index="1"></td>
    <td>1</td>
    <td>ECOPRO Co., Ltd</td>
    <td>http://www.ecoprovn.com</td>
  </tr>
  <tr onclick="changeColorRow(2)">
    <td><input type="checkbox" class="checkbox" data-index="2"></td>
    <td>2</td>
    <td>WAVINA.COM</td>
    <td>http://www.wavina.com</td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda