Mickel
Mickel

Reputation: 6696

JSTL if-statement inside HTML-attribute

is it possible to do something like this in JSTL:

<div class="firstclass<c:if test='true'> someclass</c:if>">
   <p>some other stuff...</p>
</div>

Is there any way to get this to work, or is there a better way to add a class by looking at a JSTL-if-statement?

Upvotes: 56

Views: 59168

Answers (4)

Michelle
Michelle

Reputation: 103

For me the following worked in combination with Java:

<%
String cssClass = 'oneClass';

if(i >= 5){
   cssClass += " anotherClass";
}

<div cssClass="<%= cssClass %>" >... </div>
%>


Upvotes: -1

elciospy
elciospy

Reputation: 256

That works for me!

<div id="loginById" style="height:<% out.print(instanceClass.booleanMethod()? "250px": "150px");%>">

Upvotes: -5

runfa
runfa

Reputation: 2054

It's also possible to use an EL expression directly like this:

<div class="${booleanExpr ? 'cssClass' : 'otherCssClass'}">
</div>

Upvotes: 147

simon
simon

Reputation: 12902


<c:if test='true'> 
  <c:set value="someclass" var="cssClass"></c:set>
</c:if> 
<div class="${cssClass}">
   <p>some other stuff...</p>
</div>

Upvotes: 43

Related Questions