how to keep the selected value in the dropdownlist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vigneshm0601
    New Member
    • Jan 2015
    • 2

    #1

    how to keep the selected value in the dropdownlist

    Code:
    <option value="-select">Select</option> <script type="text/javascript">
    var YearList = 5; // Specify number of "year" selections.
    var year = new Date().getFullYear();
    var pryr1 = year-2;
    var pryr = year-1;
    document.write('<option value="' + pryr1 + '">' + pryr1 + '</option>'); 
    document.write('<option value="' + pryr + '">' + pryr + '</option>'); 
    for(var i=0;i<YearList;i++){
        var curyr = year + i;
        document.write('<option value="' + curyr + '">' + curyr + '</option>'); 
    }
    </script>
    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
    Last edited by Rabbit; Jan 6 '15, 03:54 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • jmrker
    New Member
    • Dec 2012
    • 17

    #2
    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.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> Range of Years </title>
    
    </head>
    <body>
     From: <select id="SBox1" onchange="initBox2('SBox2',this.value)"></select>
     To: <select id="SBox2"></select>
    
    <script type="text/javascript">
    // From: http://www.dreamincode.net/forums/topic/341931-double-combo-box-year-range-problem/
    
    function createRange(min, max) {
        var range = [];
        for (var i = min; i <= max; i++) { range.push(i); }
        return range;
    }
    var StartYear = [];
    var EndYear = [];
    var date = new Date();
    
    function initBoxes(sbox,arr) {
      var Level1=document.getElementById(sbox);
      Level1.options.length = 0;
      for (i=0; i<arr.length; i++) {
        var x=document.createElement('option');
        var y=document.createTextNode(arr[i]);
        if (window.attachEvent) { x.setAttribute('value',y.nodeValue); }  // for IE
        x.appendChild(y);
        Level1.appendChild(x);
      }
    }
    function initBox2(sbox,newStart) {
      EndYear = createRange(newStart,date.getFullYear()+1).reverse();  // plus next year
    //  EndYear = createRange(newStart,date.getFullYear()).reverse();  // to current year
      initBoxes('SBox2',EndYear);
    }
    window.onload = function() {
      StartYear = createRange(1925,date.getFullYear()+1).reverse();
    //  StartYear = createRange(1925,date.getFullYear()).reverse();
      initBoxes('SBox1',StartYear);
    }
    </script>
    
    </body>
    </html>

    Comment

    Working...