By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

how to keep the selected values in the drop down list using javascript

P: 2
Hi, Here in the below code i need to keep the selected value to be shown. But if i select and update the page the list box always shows the index value "select".I want it to show the selected value. what changes i have to make in the below code



Expand|Select|Wrap|Line Numbers
  1. <select name="year" id="year<%=gfimCount%>"  class="textbox" style="width:60px;" >
  2.   <option value="select">Select</option>
  3.           <script type="text/javascript">
  4.            var YearList = 6; //  number of "year" selections.
  5.            var year = new Date().getFullYear();
  6.                 for(var i=5;i>0;i--){
  7.                var previousyr=year-i;
  8.                document.write('<option value="' + previousyr + '">' + previousyr + '</option>'); 
  9.                 }
  10. //document.write('<option selected="true" value="'+previousyr+'">'+previousyr+'</option>');
  11.                for(var i=0;i<YearList;i++){
  12.                var currentyr = year + i;    
  13.                document.write('<option value="' + currentyr + '">' + currentyr + '</option>');     //document.getElementBbyId(<%="currentyr"%>).value;
  14.                }
  15. //document.write('<option selected="true" value="'+currentyr+'">'+currentyr+'</option>');
  16.         </script>
  17. </select>
Jan 13 '15 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
But if i select and update the page the list box always shows the index value "select".
naturally, JavaScript is reset on every page load. (otherwise you could do some nice hacking attacks (like a key logger))

what you can do though is saving the selected value in a session (cookie, WebStorage) and load it off that store on every page load.
Jan 13 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.