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>
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>
Share
Improve this question
edited Jul 22, 2013 at 5:35
Nondeterministic narwhal
4805 silver badges17 bronze badges
asked Jul 22, 2013 at 5:17
Aman KumarAman Kumar
3534 gold badges5 silver badges11 bronze badges
3
- a very first thing is there is problem in closing form tag. close form tag properly. – Hemant Metalia Commented Jul 22, 2013 at 5:21
- nO Problem with that i am able to On select change value and Print it But unable to set value – Aman Kumar Commented Jul 22, 2013 at 5:26
- Boss 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 Commented Jul 22, 2013 at 6:13
3 Answers
Reset to default 4Try 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);
%>
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>
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.