Estoy creando una tabla muy simple que tiene dos columnas 1. Text 2. Checkbox de verificación. Quiero que cada vez que marque la casilla de verificación, se cambie el color del texto de la fila correspondiente. No sé cómo hacer eso. ¿Alguien puede guiarme, por favor?
Lo intenté de esta manera pero no funcionó. aquí está el código:
<?php $var=1; $cbid = "chechbox".$var; echo"<table> <tr> <th>Text</th> <th>Checkbox</th> </tr>"; while($var<=3){ echo" <tr> <td> <input type='text' id='$var' value='$var'> </td> <td> <input type='checkbox' id='$cbid' onclick='fun()'> </td> </tr> </table> <script> function fun(){ var a = document.getElementById($var); var b = document.getElemeentById($cbid); if(b.checked == true){ a.style.color='red'; } } </script> "; $var++; $cbid = 'chechbox'.$var; } ?>soy principiante Ni siquiera estoy seguro de si puedo hacer múltiples secuencias de comandos usando while loop.
Puedes hacerlo:
document.getElementById('check').onchange = function () { let textVar = document.getElementById('tableText'); if (document.getElementById('check').checked) { textVar.style.color = 'red'; } else { textVar.style.color = 'green'; } } <table> <tr> <th>Text</th> <th>Checkbox</th> </tr> <tr> <td><input type='checkbox' id='check' value='$var'></td> <td><input type='text' id='tableText' value='$var'></td> </tr> </table>