How to set value on select change of drop down in JSP
Solution 1
1st thing first, If you want to set value to the dropdown on change of value, well it does that itself. But if you want to set the value selected to session, again it does that itself all you have to do is use request.getParameter(color) on the next page or backend where you processing request. For printing on console, you have correct code. Only close your tags properly.
Solution 2
Try This and let me know..
<script type="text/javascript">
function setValue(){
document.getElementById("dropdown").value=document.getElementById("colour").value;
document.productForm.submit();
return true;
}
</script>
<form method="post" action="index.jsp" name="productForm">
<select id="colour" name="colour" onchange="return setValue();">
<option value="dropdown">Pls select one
<option value="apple">Apple
<option value="oragne">Orange
<option value="grapes">Grapes
</select>
<input type="hidden" name="dropdown" id="dropdown">
<input type="submit" value="click" name="btn_dropdown">
<form>
<%
String colour = request.getParameter("colour").toString();
out.println(colour);
%>
Solution 3
form
and all option
tags are not closed
Example for correct markup of option
:
<option value="apple">Apple</option>
Your updated code should be something like this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="index.jsp" name="productForm">
<select name="colour" id="dropdown">
<option value="dropdown">Pls select one</option>
<option value="apple">Apple</option>
<option value="oragne">Orange</option>
<option value="grapes">Grapes</option>
</select>
<input type="submit" value="click">
</form>
<%
String colour = request.getParameter("colour");
out.println(colour);
%>
<script>
document.getElementById("dropdown").value = '<% out.print(colour); %>';
</script>
</body>
</html>
Aman Kumar
Updated on July 11, 2022Comments
-
Aman Kumar almost 2 years
This is my code for index.jsp. I want it so that when I select an option in the drop-down menu, the value should be printed out and also the value should be set. For example, if we select "grapes" then it should print Grapes and set the value to Grapes. I have tried many things but have been unable to do so.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form method="post" action="index.jsp" name="productForm"> <select name="colour" onchange="document.productForm.submit();"> <option value="dropdown">Pls select one <option value="apple">Apple <option value="oragne">Orange <option value="grapes">Grapes </select> <input type="hidden" name="dropdown" id="dropdown"> <input type="submit" value="click" name="dropdown" id="dropdown"> <form> <% String colour = request.getParameter("colour"); out.println(colour); %> </body> </html>
-
Aman Kumar almost 11 yearsi creating Jsp so please Implement my code i want set value after onselect change
-
Manoj Yadav almost 11 yearsI have added updated code in answer, See
Your updated code should be something like this:
-
Aman Kumar almost 11 yearsorg.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 31 Error is coming
-
Aman Kumar almost 11 yearsBut i want that when i select change Drop down value should set what Ever i select
-
Aman Kumar almost 11 yearsplease see my code and please Update so that once i ll select change it should Set value after Submit
-
Sandiip Patil almost 11 yearsI suppose you will go to next page after submit, or go to some servlet to process your request. In that case you don't need to set it here you need to catch it there using the name of your dropdown.
-
Sandiip Patil almost 11 yearsedited your code to close the tags properly. Now on the second page after submit, to get the value of your dropdown do : request.getAttribute('colour'); This should work
-
Manoj Yadav almost 11 yearsAnswer updated, added
<script>document.getElementById("dropdown").value = '<% out.print(colour) %>';</script>
andid
inselect
<select name="colour" id="dropdown">
-
Aman Kumar almost 11 yearsBut i Need to to take action in same Page
-
Sandiip Patil almost 11 yearsEven in that case. what are you using to take action? Ajax? you will get the value using getParameter() of request object. and if your page is getting refreshed and you not able persist the value to the dropdown, you may use any of the javascript, jquery functions which are suggested by ppl in other answers.
-
Aman Kumar almost 11 yearsIts not working again value set to please select to One after submit
-
Aman Kumar almost 11 yearsManoj please check i am having Problem i have tried this from last 3 days But still having Problem
-
Aman Kumar almost 11 yearsi have Tried much but am Unabl to do this please help i have tried last 1 week this Problem still i dint get solution for this
-
Sandiip Patil almost 11 yearsBoss how will can you have the dropdown selected if your action is same as your jsp. The same jsp gets loaded and thats why you getting the default value for dropdown.
-
Sandiip Patil almost 11 yearsYou need to change your action, its pointing to same jsp. What action you wanna do on submit??
-
Aman Kumar almost 11 yearsThis Problem is coming only with drop down But in other i have like text field i have Print same value on action fire so i am finding such type of Solution
-
Aman Kumar almost 11 yearsManoj can i have ur email id so that i can send u My source code
-
Sandiip Patil almost 11 yearsWhat exactly you are doing when you say you are handling the request on same jsp? what is your backend processing after submitting the request.
-
Aman Kumar almost 11 years182.18.132.100:8088/cbdtforprsite/globalsearch.aspx? please check this i have to COnvert this Module in Jsp Every thing I did Except set that Drop down action is firning On select change But not Value set
-
Aman Kumar almost 11 years182.18.132.100:8088/cbdtforprsite/globalsearch.aspx? please check this i have to COnvert this Module in Jsp Every thing I did Except set that Drop down action is firning On select change But not Value set
-
Sandiip Patil almost 11 yearsText box keeps the cache value, dropdown works differently. And i am sure you not submitting the page on change for text box. here you are submitting thats why its not working.
-
Sandiip Patil almost 11 yearsThe url you have given are two sections, lower section is another jsp included in parent jsp. its using ajax to do the action and calling a servlet for that, the response is set to lower jsp. the dropdown section is getting updated with the value added to textbox. It keeps the previous value in variable and sets it with same
-
Sandiip Patil almost 11 yearswell you need to see how ajax works, and how jsps are included into each other. And if your jsp page is simple and not much values then you can create one more jsp similar to your index.jsp, result.jsp, which will be your action on this page. On result.jsp you get values from request and set to the textboxes or dropdowns over there
-
Ravi almost 11 yearsFirst check whether it has any value or not ? with the simple jsp code.Then, include your jasper reporting code. So,check your code properly, it seems, it worked !! but you have some coding error in your reports.
-
Manoj Yadav almost 11 years
;
was missing changedout.print(colour)
toout.print(colour);
, try updated code -
Aman Kumar almost 11 years