JavaLover
JavaLover

Reputation: 11

How to remove an element from any list object while iterating through <c:forEach> tag in <select> tag in JSP?

How to remove an element from any list object while iterating through <c:forEach> tag in <select> tag in JSP?

Sample code in JSP:

<select> 
  <option value="0">Select</option> 
  <c:forEach items="${list}" var="someList">
    <option value="${someList.value}">${someList.displayText}</option>
  </c:forEach>
</select>

The list object coming from Spring controller stored in model object.

Now someList.value and someList.displayText both the values are the same.

Example:

[iphone,samsung,lenovo,motog,oneplus]    

I want to remove iphone from this.

Upvotes: 1

Views: 1423

Answers (1)

Roman C
Roman C

Reputation: 1

You can't remove the item in the c:forEach tag but you can use c:if tag to filter 'iphone' from the options.

<select> 
  <option value="0">Select</option> 
  <c:forEach items="${list}" var="someList"> 
   <c:if test="${someList.value != 'iphone'}">   
    <option value="${someList.value}">${someList.displayText}</option>  
   </c:if>
  </c:forEach> 
</select>

Upvotes: 2

Related Questions