1

This is going to be a stupid question but i need to ask it. PS: this is an old practice but i need it this way. In scriptlet I have a variable status.

 <%
   String status=bean.getStatus();
   //Status value is coming from database which is either "1" or "0"(String type)
 %>

In a form I have radio button.

<tr>
<td> Status :</td>
<td>Active
            <input type="radio" name="status" checked="checked" id="statusAct" value="1" />
    Inactive
           <input type="radio" name="status" id="statusInac" value="0"/>
</tr>

If status==1 I want radio button with id="statusAct" to be selected and if status==0 then radio with id="statusInac" to be selected.

I can use javascript or Jquery but I don't know how to put Java variable inside javascript function.

1
  • 1
    Maybe var status = "<% string......%>"; and then if(status == '1') { $('#id').prop('checked', true); } Commented Jun 23, 2013 at 21:16

2 Answers 2

2

You can simply do

<input type="radio" name="status" <% ${bean.getStatus() == '1' ? 'checked="checked"' : '' } %> id="statusAct" value="1" />

and

<input type="radio" name="status" <% ${bean.getStatus() == '0' ? 'checked="checked"' : '' } %> id="statusInac" value="1" />
Sign up to request clarification or add additional context in comments.

1 Comment

Hello, I was trying to use it for input parameter from URL but getting error Syntax error on token "$", AssignmentOperator expected after this token See my syntax: <input type="radio" name="options" value="tracking" autocomplete="off" <% ${param.options == 'tracking' ? 'checked="checked"' : '' } %> >
0

Either use scriptlets to do conditional logic and write checked ="checked" where you need to. In the scriptlets you can do out. println to write to the page (output stream). You could also use jstl for condition tags c:if

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.