0

I need to use the 'checked' option in a radio button, but I want to use it dynamically. That is, either the right or the left button is pre-checked based on data from a database and the user can choose to accept or change it. To do this, I need the String "checked" as part of my radio button tag. I created two variables, chekR and chekL, with the plan that one of them would evaluate to "" while the other would evaluate to " checked". However, my IF condition fails to do the job. The value Right or Left comes from a form, and I tried printing it, which works fine, but the actual comparison is failing.

The variable passed from the form is stored as String D. If I try evaluating (D == "Right"), it returns a false every time. I tried two IFs (see code), I tried IF-ELSE, but no success.

String chekR = "";
String chekL = "";
if(D == "Left") { chekL = " checked";}
if(D == "Right") { chekR = " checked";}
<form action = update3.jsp><br>
<input type="radio" name="PtEye" value="Right" <%=chekR%>>Right
<input type="radio" name="PtEye" value="Left" <%=chekL%> required>Left
<br><input type="submit" value="Confirm"><input type="reset" value="Restore to original"></form>

I need the expression <%=chekL%> to produce either a blank or the word checked, depending upon the value of D.

Ivar
  • 6,138
  • 12
  • 49
  • 61

1 Answers1

1

In addtion to the operator issue, did you write your java code inside <% %> block?

<%
String chekR = "";
String chekL = "";
if(D == "Left") { chekL = " checked";}
if(D == "Right") { chekR = " checked";}
%>
Rasha Elsayed
  • 660
  • 1
  • 7
  • 22
  • Yes. That I did. But I did not include it in the code snippet I posted. Still, sometimes it's the littlest things that get you. :) – saurabh sawhney Sep 04 '19 at 12:08