-3
<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;
Antonio Ruiz
  • 145
  • 7
  • 3
    You should not have multiple elements with the same ID, `id` should always be unique. Use classes for this – Carsten Løvbo Andersen Sep 10 '20 at 07:22
  • Fixed, and now? – Antonio Ruiz Sep 10 '20 at 07:26
  • Mayhap `valor.RESPUESTA` is a string and always truthy. Also [getElementsByName](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName) returns a `NodeList` (or `HTMLCollection`) and not a single element. You will have to set the `checked` property on individual elements of those list, using loops or indices. – Lain Sep 10 '20 at 07:26
  • I change getElementsByName for getElementByName, and now i should cast valor.RESPUESTA to 1 or 0 ? – Antonio Ruiz Sep 10 '20 at 07:29
  • There is no `getElementByName()`. Unless you defined it yourself. Try `document.getElementByName('verificado_'+valor.ID_PREGUNTA)[0].checked = valor.RESPUESTA;` – Lain Sep 10 '20 at 07:30
  • You might need to add complete Javascript `code` to see whats going on like what is `valor.RESPUESTA` ? – Always Helping Sep 10 '20 at 07:30

1 Answers1

1

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
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77