how to retain dropdown selected value after onchange in JSP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolcoder
    New Member
    • Sep 2014
    • 2

    how to retain dropdown selected value after onchange in JSP

    I have a dropdown list. When I select a value, the page is refreshed. Then, the combo box is still showing the default value. What I want is to maintain the selected value inside combo box even after onchange.
    Here is the code ....

    Code:
    <select size="1" name="ddlSelect" id="ddlSelect" onchange="fnChange(this.value)" > 
    <option value="0">--- Select One ---</option> <%  
    	String selectedname="";                          
    	ArrayList<Profile> userlist = Controller.UserNameList(Profile.userId,year);                                
    	for (Profile j : userlist) {         
    		selectedname = (!cbmUtil.isNull(j.userName)) ? "selectedname" : "";
    		out.write("<option " + selectedname + " value='" + j.userName + "'>" + j.userName + "</option>");
    	}
    %> </select> <script type="text/javascript">
    function fnChange(Index){
    		var userName = $("#ddlSelect option:selectedname").val();                                
    		if(!userName){return false;}   
    			document.location.href="testpage.jsp?fn=pChange&userName="+userName;
    		}
    </script>
    Last edited by coolcoder; Sep 9 '14, 09:43 AM. Reason: Details explaination
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the appropriate attribute is called selected not "selectedna me".

    Comment

    • coolcoder
      New Member
      • Sep 2014
      • 2

      #3
      Dormilich,

      Thank you for your reply.
      Sorry, it is just typo error. Although it is with option:selected , result is the same.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        moving to java (jsp) forum.

        Comment

        Working...