Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  65] [ 1]  / answers: 1 / hits: 33584  / 12 Years ago, tue, february 12, 2013, 12:00:00

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!


More From » javascript

 Answers
284
$(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


[#80275] Monday, February 11, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alexandreah

Total Points: 720
Total Questions: 85
Total Answers: 90

Location: Central African Republic
Member since Fri, Jun 5, 2020
4 Years ago
;