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

how to keep the selected value in the dropdownlist

P: 2
Expand|Select|Wrap|Line Numbers
  1. <option value="-select">Select</option> <script type="text/javascript">
  2. var YearList = 5; // Specify number of "year" selections.
  3. var year = new Date().getFullYear();
  4. var pryr1 = year-2;
  5. var pryr = year-1;
  6. document.write('<option value="' + pryr1 + '">' + pryr1 + '</option>'); 
  7. document.write('<option value="' + pryr + '">' + pryr + '</option>'); 
  8. for(var i=0;i<YearList;i++){
  9.     var curyr = year + i;
  10.     document.write('<option value="' + curyr + '">' + curyr + '</option>'); 
  11. }
  12. </script>
  13.  
by having this code the result shown in the drop down list is
2013
2014
2015
2016
2017
2018
2019
if i select any year over here and update the page, the value in the drop down box comes and again to "select"
i want the year which i selected to be shown
Jan 6 '15 #1
Share this Question
Share on Google+
1 Reply


P: 17
Don't use "document.write()" to create your drop-down selections.
As soon as you re-enter the function, it reloads the page to it's original contents.

Modify the following to meet your needs or come back with questions.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title> Range of Years </title>
  6.  
  7. </head>
  8. <body>
  9.  From: <select id="SBox1" onchange="initBox2('SBox2',this.value)"></select>
  10.  To: <select id="SBox2"></select>
  11.  
  12. <script type="text/javascript">
  13. // From: http://www.dreamincode.net/forums/topic/341931-double-combo-box-year-range-problem/
  14.  
  15. function createRange(min, max) {
  16.     var range = [];
  17.     for (var i = min; i <= max; i++) { range.push(i); }
  18.     return range;
  19. }
  20. var StartYear = [];
  21. var EndYear = [];
  22. var date = new Date();
  23.  
  24. function initBoxes(sbox,arr) {
  25.   var Level1=document.getElementById(sbox);
  26.   Level1.options.length = 0;
  27.   for (i=0; i<arr.length; i++) {
  28.     var x=document.createElement('option');
  29.     var y=document.createTextNode(arr[i]);
  30.     if (window.attachEvent) { x.setAttribute('value',y.nodeValue); }  // for IE
  31.     x.appendChild(y);
  32.     Level1.appendChild(x);
  33.   }
  34. }
  35. function initBox2(sbox,newStart) {
  36.   EndYear = createRange(newStart,date.getFullYear()+1).reverse();  // plus next year
  37. //  EndYear = createRange(newStart,date.getFullYear()).reverse();  // to current year
  38.   initBoxes('SBox2',EndYear);
  39. }
  40. window.onload = function() {
  41.   StartYear = createRange(1925,date.getFullYear()+1).reverse();
  42. //  StartYear = createRange(1925,date.getFullYear()).reverse();
  43.   initBoxes('SBox1',StartYear);
  44. }
  45. </script>
  46.  
  47. </body>
  48. </html>
  49.  
  50.  
Jan 6 '15 #2

Post your reply

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