Issue
I have a dropdown menu and i want when selecting an item from menu just to show it below. This is my jsp:
<body>
<select name="courseSelect">
<c:forEach var="course" items="${courses}">
<option name="courseOption" value="${course.name}">${course.name}</option>
</c:forEach>
</select>
${courseSelect} //for example to show it here
</body>
And this is my servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
String courseSelect = request.getParameter("courseSelect");
session.setAttribute("courseName", courseSelect);
session.setAttribute("courses", Course.allCourses());
RequestDispatcher rd = request.getRequestDispatcher("home.jsp");
rd.forward(request, response);
}
I tried to get parameters from select tag and also from option tag but it doesn't show anything. How can i solve this?
Solution
Javascript might be better to handle this case on front-end itself.you can change your code as below for Vanilla Javascript
<body>
<select id="selectBox" name="courseSelect" onchange="changeText()">
<c:forEach var="course" items="${courses}">
<option name="courseOption" value="${course.name}">${course.name}</option>
</c:forEach>
</select>
<p id="showValue"></p> //for example to show it here
<script>
function changeText(){
var selectedValue = document.getElementById('selectBox').value;
document.getElementById('showValue').innerText = selectedValue;
}
</script>
</body>
Answered By - Nagesh Tripathi