#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Dropdown dynamically current year and previous year in html


    <select name="YEAR" id="YEAR" >
    <option value="" >Select the Year</option>
    <option value="2012" >2012</option>
    <option value="2013" >2013</option>
    </select>

    i want to display first "Select the Year" when html page is opened and in that dropdown current year and previous year dynamically in that
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,118
    Rep Power
    119
    Code:
    <script>
    function insertYears()
    {
    var now = new Date();
    var thisYear = now.getFullYear();
    var lastYear = parseInt(now.getFullYear()) - 1;
    var new_opt_one = document.createElement("option");
    var new_opt_two = document.createElement("option");
    new_opt_one.value = thisYear;
    new_opt_one.innerHTML = thisYear;
    new_opt_two.value = lastYear;
    new_opt_two.innerHTML = lastYear;
    document.getElementById("YEAR").appendChild(new_opt_two);
    document.getElementById("YEAR").appendChild(new_opt_one);
    }
    window.onload = function() {
    insertYears();
    }
    </script>
    
    <select name="YEAR" id="YEAR" >
    <option value="" >Select the Year</option>
    </select>

IMN logo majestic logo threadwatch logo seochat tools logo