Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote this script to bind 1-100 numbers to asp.net dropdown list. But dropdown list is not showing any data .I have attached screen shot of the drop down. What is wrong in below code

What I have tried:

JavaScript
function loaddropdown()
{
    var dropdown = document.getElementById("ddFAge");
    
    for(var i=1;i<=100;i++)
    {
        var newOption = new Option[![enter image description here][1]][1];
        newOption=document.createElement(option);
        newOption.Text = i;
        newOption.value = i;
        dropdown.options[i] = newOption;
        
        //dropdown.options.add(newOption);

        //<option value="0"><--Select Age--></option>
    }
}
window.onload=loaddropdown();
Posted
Updated 26-Aug-20 20:02pm
v2

1 solution

Try change the newOption initialization and text assignment:
JavaScript
function loaddropdown()
{
    var dropdown = document.getElementById("ddFAge");
    
    for(var i=1;i<=100;i++)
    {
        // var newOption = new Option[![enter image description here][1]][1];
        var newOption=document.createElement("option");
        newOption.innerHTML = i;  //innerHTML & not Text
        newOption.value = i;
        dropdown.options.add(newOption);
        
        //dropdown.options.add(newOption);

        //<option value="0"><--Select Age--></option>
    }
}

BTW, I am not sure where from you are using the value of i here. Above code would iterate 100 times and add the same thing to dropdown 100 times. I doubt if that is what you are trying here. Do check.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900