how to dynamically bind Year in DropDownList using JavaScript

Introduction: Hello Guys, “Welcome back” Today i am here with another one new great article. In this article I will explain about how to dynamically bind or populate Year in DropDownList i.e. HTML SELECT element using JavaScript.

Inside the window.onload event handler, the DropDownList i.e. HTML SELECT element will be referenced and using a For Loop, one by one Year values will be appended to the DropDownList using JavaScript.

Code for HTML page

On the  HTML page I place a  DropDownList i.e. HTML SELECT element.

<select id="ddlYears"></select>

 

 

Dynamically populating Year in DropDownList (SELECT) using JavaScript

On the window.onload event handler of the page, first the DropDownList i.e. HTML SELECT element is referenced and then the Current Year will be find using JavaScript Date object.

Then using a For Loop, one by one Year values will be increasing or appended to the DropDownList by creating a dynamic OPTION element using JavaScript.

<script type="text/javascript">

    window.onload = function () {

        //Reference the DropDownList.

        var ddlYears = document.getElementById("dropdownlist_years");

 

        //Determine the Current Year.

        var currentYear = (new Date()).getFullYear();

 

        //Loop and add the Year values to DropDownList.

        for (var i = 1950; i <= currentYear; i++) {

            var option = document.createElement("OPTION");

            option.innerHTML = i;

            option.value = i;

            ddlYears.appendChild(option);

        }

    };

</script>

 

Here I am giving complete code for HTML page. You need to copy and paste the code in your HTML page for enjoying the code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

</head>

<body>

    Select you DOB<select id="dropdown_Years">

    </select>

    <script type="text/javascript">

        window.onload = function () {

            //binding the DropDownList.

            var ddlYears = document.getElementById("dropdown_Years");

 

            //find the Current Year.

            var currentYear = (new Date()).getFullYear();

 

            //Loop for add the Year values to DropDownList.

            //Year started from 1950, You can set as per your requirement

            for (var i = 1950; i <= currentYear; i++) {

                var option = document.createElement("OPTION");

                option.innerHTML = i;

                option.value = i;

                ddlYears.appendChild(option);

            }

        };

    </script>

</body>

</html>

Check the Output of the complete Program

 

Conclusion: In above code, I have been explained that how to activate or enable the TextBox if other option is selected by the user from DropDownlist using JavaScript and jQuery. Bye Bye and take care of yourself Developers. We will come back shortly with the new article.

 

Regards

Using Asp.net

Comments

Popular posts from this blog

Sending reset password link for one time use only in asp.net

add delete update inside gridview using store procedure in ASP.NET

Change password using asp.net