Search code examples
javascriptjqueryradio-group

How to check second option in radio group if false?


<input type="radio" name="verificado_57" value="0" onclick="save(57)"> OK
<input type="radio" name="verificado_57" value="1" onclick="save(57)"> NO

Then, when i get response from mysql, if value is true should checked OK, if false should checked NO.

Im try this but not work.

document.getElementByName('verificado_'+valor.ID_PREGUNTA).checked = valor.RESPUESTA;

Solution

  • An answer could look something like this:

    var valor = {
      "ID_PREGUNTA": 57,
      "RESPUESTA": true
    }
    var s = document.getElementsByName('verificado_' + valor.ID_PREGUNTA);
    
    for (var i = 0; i < s.length; i++) {
      s[i].checked = valor.RESPUESTA;
    }
    

    Demo

    var valor = {
      "ID_PREGUNTA": 57,
      "RESPUESTA": true
    }
    var s = document.getElementsByName('verificado_' + valor.ID_PREGUNTA);
    
    for (var i = 0; i < s.length; i++) {
      s[i].checked = valor.RESPUESTA;
    }
    <input type="radio" name="verificado_57" value="0" onclick="save(57)"> OK
    <input type="radio" name="verificado_57" value="1" onclick="save(57)"> NO