D
D
doorway-team2021-06-09 18:30:39
JavaScript
doorway-team, 2021-06-09 18:30:39

How to highlight a table row when a checkbox is selected in a cell?

There is a table like this:

<table style="border-collapse: collapse; width: 100%;" border="1">
<tbody>
<tr>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">
<div><input class="form-check-input" type="checkbox" value="" /></div>
</td>
</tr>
<tr>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">
<div><input class="form-check-input" type="checkbox" value="" /></div>
</td>
</tr>
<tr>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">&nbsp;</td>
<td style="width: 20%;">
<div><input class="form-check-input" type="checkbox" value="" /></div>
</td>
</tr>
</tbody>
</table>

How to make it so that when the checkbox is checked, the table row tr is highlighted?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-06-09
@doorway-team

document.querySelector('table').addEventListener('change', ({ target: t }) => {
  t.closest('tr').classList.toggle('класс, стилизующий строку с отмеченным чекбоксом', t.checked);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question