Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Friday, 17 April 2015

Introduction

In this article I will explain how to use generate year dropdown dynamically in JavaScript or jQuery generate years dropdown dynamically.


Following  JQuery script is used to generate year dropdown dynamically:

<script type="text/javascript">
    $(function () {
        var noofyears = 100; // Change to whatever you want
        var thisYear = (new Date()).getFullYear();
        for (var i = 0; i <= noofyears; i++) {
            var year = thisYear - i;
            $('<option>', { value: year, text: year }).appendTo("#ddlyear");
        }
    });
</script>

Full Source Code for sample Application:
Design your Page as given Below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Generate years for dropdown</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
    $(function () {
        var noofyears = 100; // Change to whatever you want
        var thisYear = (new Date()).getFullYear();
        for (var i = 0; i <= noofyears; i++) {
            var year = thisYear - i;
            $('<option>', { value: year, text: year }).appendTo("#ddlyear");
        }
    });
</script>
</head>
<body>
<form id="form1">
 <fieldset style="width:250px">
    <legend><strong>Generate years for dropdown</strong></legend>
<div>
<br />
Select year: <select id="ddlyear"></select>
<br><br />
</div>
</fieldset>
</form>
</body>

</html>
Categories: ,

0 comments:

Post a Comment