6

I want to populate the year in a select javascript. I did the following code.

<html>
<head>
    <script type="text/javascript">         
        $(document).ready(function(){   
            var cur_year=new Date().getFullYear();
            var obj=document.getElementById("yr");  
            alert(obj);         
            for (var i = 2000; i <= 2020; i++)     {                
                opt = document.createElement("option");
                opt.appendChild(document.createTextNode(value));
                opt.selected = selected;
                opt.value = i;
                opt.text=i;
                obj.appendChild(opt);
            }
        });
    </script>
</head>
<body>
    <select id="yr">
<option>year</option>
</select>
</body>
</html>

I don't know what is wrong in this. I want to populate the year and want to select the current year in the select box when the user opens the browser. Any one can help? please!

2
  • where is that value variable ? Commented Feb 12, 2013 at 7:04
  • if i delete that line- opt.appendChild(document.createTextNode(value()), what will happen? Commented Feb 12, 2013 at 7:06

2 Answers 2

7
$(document).ready( function() { 
            var cur_year=new Date().getFullYear();
            var obj=document.getElementById("yr");         
            for (var i = 2000; i <= 2020; i++)     {                
                opt = document.createElement("option");
                opt.value = i;
                opt.text=i;
                obj.appendChild(opt);
            }
            document.getElementById("yr").value = cur_year;
        });
  1. Just Use .value and assign cur_year to it.

Working Fiddle

Sign up to request clarification or add additional context in comments.

5 Comments

Can you please tell the same code, without using the jquery. want to use javascript only.
@ijarlax see my updated answer it is just without IF statement.
@AspiringAqib : This is a place for giving people solution and getting solutions from others. Not a place for fighting like kids. I appreciate your answer, but if you don't learn to respect others and accept that someone else can also have the solution without your help, you cant get much progress..
@ijarlax : You can use $("#yr") in place of document.getElementById("yr")
@Avishek he wants pure JS as he commented above and ijarlax how it is possible? IE atleast supports document.getElementById
-1
<?php 
$current_year = date('Y');
$end_year = date('Y', strtotime('+10 years'));
?>
<label>Year</label><select name="year">
    <?php
for($i=$current_year; $i<=$end_year; $i++)
{
    ?>
    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
    <?php
}
?>
</select>

1 Comment

You should explain, what you did there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.