I am finishing a project and would like to see how to enable and disable a text field depending on the radiobutton that the user selects.
I already found a code in the forum and it is what I want only that my questionnaire is filled with an array that brings me the questions of a database and this causes that when selecting a "no" I enabled all the fields and I just want you to enable the text field of the question where the user said "no".
this is my form
<?php
//consulta que captura el texto , id de la tabla respuestas
$sql = "SELECT texto,id FROM respuestas WHERE idenc='$id'";
$sql = mysqli_query($conexion,$sql);
//ahora recorremos los datos texto, id que estan vinculadas a la cuenta seleccionada
while ($row = mysqli_fetch_array($sql)){
$texto = $row["texto"];
$idres = $row["id"];
?><tr>
<!-- <td width="50"><input type="radio" name="opcion" value="<?php echo $idres; ?>" required</td>
<td width="470"><?php echo $texto; ?></td>-->
<?php echo "<fieldset>
<legend>".$texto."</legend>
<label>
<input type='radio' name='radio".$idres."' value='1' onclick='deshabilita()'> Si
</label>
<label>
<input type='radio' name='radio".$idres."' value='0' onclick='habilita()'> No
</label>
<label>
<textarea name='Hallazgo".$idres."' disabled class='inputText'>Se tiene un hallazgo? </textarea>
</label>
<label>
<textarea name='Accion".$idres."' disabled class='inputText'>Cual es la acción correctiva </textarea>
</label>
</fieldset>"; ?>
</tr>
<?php } ?>
<tr>
this is the javascript code
<script language="JavaScript">
function habilita(){
elementos=document.getElementsByClassName("inputText");
for(var i = 0; i < elementos.length; i++)
{
elementos[i].disabled = false;
}
}
function deshabilita(){
elementos=document.getElementsByClassName("inputText");
for(var i = 0; i < elementos.length; i++)
{
elementos[i].disabled = true;
}
}
</script>