Sneha Raju
asked on
How to retain the dropdown value after submitting the form
I had this question after viewing Retain the value of Drop down box in jsp.
I tried the below code but i'm getting error : alloc_Type is undefined on refresh.
<%
String riskTypeValue = request.getParameter("risk Type");
if(riskTypeValue == null)riskTypeValue = "0";
String allocTypeValue = request.getParameter("allo cType");
if(allocTypeValue == null)allocTypeValue = "0";
%>
<script>
function retainValue(){
//alert("retainValue "+alloc_Type +" && " +risk_Type);
var element1 = document.forms[0].allocTyp e;
for(x = 0; x < element1.length; x++){
if(element1.options[x].val ue == "<%=alloc_Type%>")
{
element1.options[x].select ed = true;
break;
}
}
var element2 = document.forms[0].riskType ;
for(x = 0; x < element2.length; x++){
if(element2.options[x].val ue == "<%=risk_Type%>"){
element2.options[x].select ed = true;
break;
}
}
}
</script>
<tr>
<td> </td>
<td class="smalltext" width="150px"><b>Allocatio n Type:</b></td>
<td class="smalltext">
<select id="allocType"
class="filter_smalltext" name="allocType">
<c:forEach items="${dataBO.alloc_type s}" var="allocTypes">
<option value="${allocTypes.key}" ${ allocTypes.key eq dataBO.alloc_type ? 'selected':'' }>
<c:out value="${allocTypes.value} "></c:out>
</option>
</c:forEach>
</select>
</td>
<td> </td>
</tr>
I tried the below code but i'm getting error : alloc_Type is undefined on refresh.
<%
String riskTypeValue = request.getParameter("risk
if(riskTypeValue == null)riskTypeValue = "0";
String allocTypeValue = request.getParameter("allo
if(allocTypeValue == null)allocTypeValue = "0";
%>
<script>
function retainValue(){
//alert("retainValue "+alloc_Type +" && " +risk_Type);
var element1 = document.forms[0].allocTyp
for(x = 0; x < element1.length; x++){
if(element1.options[x].val
{
element1.options[x].select
break;
}
}
var element2 = document.forms[0].riskType
for(x = 0; x < element2.length; x++){
if(element2.options[x].val
element2.options[x].select
break;
}
}
}
</script>
<tr>
<td> </td>
<td class="smalltext" width="150px"><b>Allocatio
<td class="smalltext">
<select id="allocType"
class="filter_smalltext" name="allocType">
<c:forEach items="${dataBO.alloc_type
<option value="${allocTypes.key}" ${ allocTypes.key eq dataBO.alloc_type ? 'selected':'' }>
<c:out value="${allocTypes.value}
</option>
</c:forEach>
</select>
</td>
<td> </td>
</tr>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I believe I answered the question.
Every other line of code is talking of allocType and allocTypes without underscore, why should <%=alloc_Type%> work, if no previousline defines something like that?
In one of the first lines you define: String allocTypeValue = request.getParameter("allo
Bye, Olaf.