Search code examples
javascriptjquerystruts-1

Get radio button checked by clicking on a button


Update Hello everyone

As you can see in the picture, I have three radio buttons :

<html:radio  property= "workerGender" value="unknown" styleId="unknown"/>
<html:radio  property= "workerGender" value="woman" styleId="woman"/>
<html:radio  property= "workerGender" value="man" styleId="man"/>

In the popup, I have the gender field :

<html:text style="border:0;" property="workerGender" readonly="true" styleId="wkGenderRN"/>

You can see that the gender is not the same between the left part and the right part (on the left, it's unknown and on the right, it's male). What I'd like to do but can't do is this : when I click on the left button in the popup, I'd like the gender to be updated in the left part (in that case, the "male" radio button must be checked after the click).

This is what I do with Javascript once I get the gender in the right part :

var genderRN = document.getElementById("wkGenderRN").value;
var gender = "";
if (genderRN == "M"){
    gender = "man";
 } else if (genderRN == "F"){
    gender = "woman";
} else {
    gender = "unknown";
}

Any ideo on how I can proceed afterwards ? Thank you in advance

P.S : I tried document.getElementById(gender).prop('checked',true); but with no success


Solution

  • In document.getElementById(gender).prop('checked',true);, you are trying to use a JQuery function on an simple element. So you can use either use the checked attribute

    document.getElementById(gender).checked = true;
    

    Or use JQuery

    $(`#${gender}`).prop('checked', true)