Javascript – How to set the end value of tag based on Selected DropDown

javascriptjspjstl

1)    <select onChange="showList(this.value);">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
    </select> 

2)<script type="text/javascript">
    function showList(value){
    var endValue=value;
    }
  </script>

what i want is I want to set the value of end is the selcted value of dropdown

3)<c:forEach begin="1" end="? Here I want to set the endValue">
    <h1>print data<h1>
   </c:forEach>

Best Solution

The <c:forEach> is a jstl tag that's evaluated on the server side, so you would need to reload the page for the user's input to affect the evaluation of that tag. You could do that with a form post when the select is changed, as in the example below. This could be done in different ways, but there's no way around the fact that you need to go back to the server.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
    function showList(){
        document.getElementById('myform').submit();
    }
</script>
</head>
<body>
    <form method="POST" id="myform">
        <select onChange="showList();" name="myselect">
            <option value="1" <c:if test="${param.myselect eq 1}">selected="selected"</c:if>>1</option>
            <option value="2" <c:if test="${param.myselect eq 2}">selected="selected"</c:if>>2</option>
            <option value="3" <c:if test="${param.myselect eq 3}">selected="selected"</c:if>>3</option>
            <option value="4" <c:if test="${param.myselect eq 4}">selected="selected"</c:if>>4</option>
        </select>
    </form>
    <c:if test="${not empty param.myselect}">
        <c:forEach begin="1" end="${param.myselect}">
            <h1>print data</h1>
        </c:forEach>
    </c:if>
</body>
</html>