Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

radio button auto check based on condition using jstl

Tags:

jstl

i need to set the radio button as checked based on the value present in the request. below is the code i used in my JSP

<input type="radio" name="status" id="status" value="Active" checked="<c:if test="${posting.postingStatus eq 'Active'}">checked</c:if>">
                Active&nbsp; 
<input type="radio" name="status" id="status" value="Closed" checked="<c:if test="${posting.postingStatus eq 'Closed'}">checked</c:if>">
                Closed

I am getting the radiobutton along with the text 'checked/>Active' and another radio button with text 'checked/>Closed'

i tried using another set of code

<c:choose>
    <c:when test="${posting.postingStatus eq 'Active'}">
    <input type="radio" name="status" id="status" value="Active" checked="checked"/>
    Active&nbsp; 
    <input type="radio" name="status" id="status" value="Closed"/>
    Closed
    </c:when>
    <c:otherwise>
    <input type="radio" name="status" id="status" value="Active" />
    Active&nbsp;
    <input type="radio" name="status" id="status" value="Closed" checked="checked"/>
    Closed
    </c:otherwise>

i am getting double times with improper result.

can anyone help me with this?

like image 632
Kalaiarasan Manimaran Avatar asked Dec 05 '22 09:12

Kalaiarasan Manimaran


1 Answers

Try this way:

    <input type="radio" name="status" id="status"
     value="Active" ${posting.postingStatus=='Active'?'checked':''}>
like image 114
Alex Avatar answered Jan 03 '23 10:01

Alex