4green
4green

Reputation: 11

check radio button on a form based on value retrieved from database

I am trying to populate the "gender" value stored in database (jsonObj.TEMP.RECS[x].GENDER) onto the HTML form. Let say if the value is "Male", I want the radio button for "Male" to be selected. Please help.

<input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female

Javascript:

document.getElementsByName("gender").value = jsonObj.TEMP.RECS[x].GENDER;

Upvotes: 1

Views: 140

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

Try to use querySelector since getElementsByName() will return array of objects and you want to check the one with specific value in jsonObj.TEMP.RECS[x].GENDER, check example bellow :

document.querySelector('input[value="'+jsonObj.TEMP.RECS[x].GENDER+'"]').checked = true;

Hope this helps.

Upvotes: 2

Related Questions