drop down box upto current year/populate with current month

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhishekbrave
    New Member
    • Dec 2007
    • 79

    drop down box upto current year/populate with current month

    i have created following drop down box for displaying year... i want to populate the drop down box upto current year.. and need to write a java script for this .. any pointers in this regard will be very helpful
    Code:
    <select name=year>
    <option value=2008>2008</option>
    <option value=2007>2007</option>
    <option value=2006>2006</option>
    </select>
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Originally posted by abhishekbrave
    i have created following drop down box for displaying year... i want to populate the drop down box upto current year.. and need to write a java script for this .. any pointers in this regard will be very helpful
    Code:
    <select name=year>
    <option value=2008>2008</option>
    <option value=2007>2007</option>
    <option value=2006>2006</option>
    </select>
    here's a start...

    Code:
    function populateYearSelect()
    {
        d = new Date();
        curr_year = d.getFullYear();
        
        for(i = 0; i < 3; i++)
        {
            document.getElementById('year').options[i] = new Option(curr_year-i,curr_year-i);
        }
    }
    [HTML]

    <body onLoad="populat eYearSelect()">

    <select name="year" id="year">
    // options generated dynamically
    </select>

    </body>
    [/HTML]

    that should be start. i just wrote it off the top of my head so take it and run with it!

    Good luck.

    Comment

    • abhishekbrave
      New Member
      • Dec 2007
      • 79

      #3
      Originally posted by dlite922
      here's a start...

      Code:
      function populateYearSelect()
      {
          d = new Date();
          curr_year = d.getFullYear();
          
          for(i = 0; i < 3; i++)
          {
              document.getElementById('year').options[i] = new Option(curr_year-i,curr_year-i);
          }
      }
      [HTML]

      <body onLoad="populat eYearSelect()">

      <select name="year" id="year">
      // options generated dynamically
      </select>

      </body>
      [/HTML]

      that should be start. i just wrote it off the top of my head so take it and run with it!

      Good luck.
      hey thanx i am able to get current year populated by doing some workaround.

      Comment

      • abhishekbrave
        New Member
        • Dec 2007
        • 79

        #4
        Populate with current month

        I have created a drop down box for displaying month.But I want to show current month in the drop down box .
        [HTML]
        <select name=month onChange="setMo nth(value)">
        <option value=01>JAN</option>
        <option value=02>FEB</option>
        <option value=03>MAR</option>
        <option value=04>APR</option>
        <option value=05>MAY</option>
        <option value=06>JUN</option>
        <option value=07>JUL</option>
        <option value=08>AUG</option>
        <option value=09>SEP</option>
        <option value=10>OCT</option>
        <option value=11>NOV</option>
        <option value=12>DEC</option>
        </select>
        [/HTML]

        Now I need to write a java script so that the drop down box will show intially the current month. For example the current month is march therefore the drop dow box should show intially march as the current month.

        Need to know how to start it with java scripts any pointers will be very helpful.

        Comment

        • abhishekbrave
          New Member
          • Dec 2007
          • 79

          #5
          Originally posted by abhishekbrave
          hey thanx i am able to get current year populated by doing some workaround.
          thanx for the previous reply the next thing i want is to show current month in the drop down box.

          [HTML]
          <select name=month onChange="setMo nth(value)">
          <option value=01>JAN</option>
          <option value=02>FEB</option>
          <option value=03>MAR</option>
          <option value=04>APR</option>
          <option value=05>MAY</option>
          <option value=06>JUN</option>
          <option value=07>JUL</option>
          <option value=08>AUG</option>
          <option value=09>SEP</option>
          <option value=10>OCT</option>
          <option value=11>NOV</option>
          <option value=12>DEC</option>
          </select>

          [/HTML]
          hope u ll help me here also
          :) thnax

          Comment

          • abhishekbrave
            New Member
            • Dec 2007
            • 79

            #6
            Hi ,

            I got the solution of this problem
            Only need to add following lines of code in the script

            curr_month = d.getMonth;
            document.month. options[curr_month].selected=true;

            Originally posted by abhishekbrave
            thanx for the previous reply the next thing i want is to show current month in the drop down box.

            [HTML]
            <select name=month onChange="setMo nth(value)">
            <option value=01>JAN</option>
            <option value=02>FEB</option>
            <option value=03>MAR</option>
            <option value=04>APR</option>
            <option value=05>MAY</option>
            <option value=06>JUN</option>
            <option value=07>JUL</option>
            <option value=08>AUG</option>
            <option value=09>SEP</option>
            <option value=10>OCT</option>
            <option value=11>NOV</option>
            <option value=12>DEC</option>
            </select>

            [/HTML]
            hope u ll help me here also
            :) thnax

            Comment

            • abhishekbrave
              New Member
              • Dec 2007
              • 79

              #7
              Hi ,

              I got the solution of this problem
              Only need to add following lines of code in the script
              Code:
              curr_month = d.getMonth;
              document.month.options[curr_month].selected=true;
              Originally posted by abhishekbrave
              I have created a drop down box for displaying month.But I want to show current month in the drop down box .
              [HTML]
              <select name=month onChange="setMo nth(value)">
              <option value=01>JAN</option>
              <option value=02>FEB</option>
              <option value=03>MAR</option>
              <option value=04>APR</option>
              <option value=05>MAY</option>
              <option value=06>JUN</option>
              <option value=07>JUL</option>
              <option value=08>AUG</option>
              <option value=09>SEP</option>
              <option value=10>OCT</option>
              <option value=11>NOV</option>
              <option value=12>DEC</option>
              </select>
              [/HTML]

              Now I need to write a java script so that the drop down box will show intially the current month. For example the current month is march therefore the drop dow box should show intially march as the current month.

              Need to know how to start it with java scripts any pointers will be very helpful.

              Comment

              Working...