0

i want to retrieve radio button value in checked radio button in jsp page

<% String gen=rs.getString("gender");session.setAttribute("gender",  gen); %>
     <input type="radio" name="gender" id="male"  value="male" />male
     <input type="radio" name="gender" id="female"    value="female" />Female
      <input type="radio" name="gender" id="other"   value="other" />
muneeb
  • 11
  • 1
  • 7

2 Answers2

1

You can use el to add checked attribute on condition

<input type="radio" name="gender" id="male" value="male" ${gen eq "male"?'checked="checked"':''}/>
<input type="radio" name="gender" id="female" value="female" ${gen eq "female"?'checked="checked"':''}/>Female
<input type="radio" name="gender" id="other" value="other" ${gen eq "other"?'checked="checked"':''}/>
fujy
  • 5,168
  • 5
  • 31
  • 50
  • can you explain it? – muneeb May 10 '17 at 17:59
  • Simply `el` provide you with a ternary operator similar to the one that you have in `Java`, I am just using it here to check if `gen` value is equal `eq` to "male", "female" or "other". If true, then I apply the `checked` attribute. If false I apply nothing as `' '` – fujy May 10 '17 at 18:05
  • Check the [`el`](https://stackoverflow.com/tags/el/info) tag wiki and this example [here](http://javabeat.net/ternary-operator-in-jsp-2-0-expression-languageel/) for more – fujy May 10 '17 at 18:06
  • @muneeb, if it answers your question, kindly mark it as accepted as [here](https://stackoverflow.com/help/accepted-answer) – fujy May 10 '17 at 18:46
0

You can check for each option if this option's value is equal to the gender. If so add checked="checked"

ex:

<input type="radio" name="gender" id="male"  value="male" <% Response.Write( (gen == "male") ? ' checked="checked"' : '' ) %> />male

or you can take a look at this question

Community
  • 1
  • 1