How to retain the dropdown value after submitting the form

Sneha Raju
Sneha Raju used Ask the Experts™
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";

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;
    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;

                        <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>
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Olaf DoschkeSoftware Developer

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.
Did you change from your original question?  
I wrote a demonstration code.  If you are using JSTL, then I can rewrite it.
    String cr = "";
	String mr = "";
	String both = "";
	String none = "";
	String selectedString = "selected=\"selected\"";
	String selectedValue = request.getParameter("riskType");
	if(selectedValue == null)none = selectedString;
	if("CR".equals(selectedValue))cr = selectedString;
	if("MR".equals(selectedValue))mr = selectedString;
	if("BOTH".equals(selectedValue))both = selectedString;
		<select name="riskType">
            <option value="" <%=none%>>Choose One</option>
            <option value="CR" <%=cr%>>CR</option>
            <option value="MR" <%=mr%>>MR</option>
            <option value="BOTH" <%=both%>>BOTH</option>
		<input type='submit' value='Submit'/>
	Selected value is <%=selectedValue%>

Open in new window

I believe I answered the question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial