Link to home
Start Free TrialLog in
Avatar of Sneha Raju
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("riskType");
if(riskTypeValue == null)riskTypeValue = "0";

String allocTypeValue = request.getParameter("allocType");
if(allocTypeValue == null)allocTypeValue = "0";
%>

<script>
function retainValue(){
      //alert("retainValue "+alloc_Type +" && " +risk_Type);
    var element1 = document.forms[0].allocType;
    for(x = 0; x < element1.length; x++){
            if(element1.options[x].value == "<%=alloc_Type%>")
            {
                  element1.options[x].selected = true;
                  break;
            }
    }
    var element2 = document.forms[0].riskType;
    for(x = 0; x < element2.length; x++){
            if(element2.options[x].value == "<%=risk_Type%>"){
                  element2.options[x].selected = true;
                  break;
            }
    }
}
</script>


<tr>
                        <td>&nbsp;</td>
                        <td class="smalltext" width="150px"><b>Allocation Type:</b></td>
                        <td class="smalltext">
                               <select id="allocType"
                                          class="filter_smalltext" name="allocType">
                                    <c:forEach items="${dataBO.alloc_types}" 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>&nbsp;</td>
                  </tr>
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

if(element1.options[x].value == "<%=alloc_Type%>")

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("allocType") so I assume you would need to use that name: allocTypeValue or <%=allocTypeValue%>, as allocTypeValue is defined.

Bye, Olaf.
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I believe I answered the question.