Empty dropdown list displaying no options

<script>
    function populateDropdown()
    {
        var dropdown = document.getElementById("ddFAge");
        
        for(var i=1;i<=100;i++)
        {
            var newOption = new Option()
            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=populateDropdown();
</script>

I designed this script to assign numbers 1-100 to an asp.net dropdown list. However, the dropdown list is not displaying any data. I have included a screenshot of the inactive dropdown. What mistake did I make in the code above?

Answer №1

There are several issues in your code. Consider the following corrections:

function createDropdown() {
    var dropdown = document.getElementById("dropdownAge");

    for (var i = 1; i <= 100; i++) {
        var newOption = document.createElement('option');
        newOption.text = i;
        newOption.value = i;
        dropdown.appendChild(newOption);
    }
}
window.onload = createDropdown();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

establish the parent child size with CSS

In my current project, I am faced with a challenge regarding the CSS and HTML code. I am attempting to have a <div> element positioned after an <img> element inherit the width and height of the image. This <div> is meant to act as an over ...

Utilize Mongo's $facet feature to retrieve and calculate the number of tags associated with the products that have been

My current aggregation pipeline looks like this: aggregate.lookup({ from: 'tags', localField: 'tags', foreignField: '_id', as: 'tags' }); aggregate.match({ productType: 'prod ...

Passing specific props to child components based on their type in a React application using TypeScript

Sorry if this question has already been addressed somewhere else, but I couldn't seem to find a solution. I'm looking for a way to pass props conditionally to children components based on their type (i.e. component type). For example, consider ...

Alert for JavaScript Increment (++) Operation

While reviewing my code using jslint, I noticed a warning regarding the increment operator: var x = 1; x++; Warning: Unexpected expression '++' in statement position. According to the documentation: "They are second only to faulty archi ...

Steps for comparing two inputs and displaying a div:1. Obtain the values of

I am looking to create a basic JavaScript function that will show or hide an element based on whether two input values match. This needs to happen before the form is submitted. <form> <input type="number" name="some1" id="some1"> <input t ...

Leveraging jQuery for Adding Text to a Span While Hovering and Animating it to Slide from Left to

<p class="site-description">Eating cookies is <span class="description-addition"></span>a delight</p> <script> var phrases = new Array('a sweet experience', 'so delicious', 'the best treat', ...

Is the syntax incorrect or is there another reason for the empty array being passed, as the "resolve" callback is running before the completion of the for loop?

The for loop will iterate over the length of req.body, executing a Customer.find operation in each iteration. The resolve function will then be called with an array containing the results of all the find operations. let promise = new Promise(function(res ...

Place a <script> tag within the Vue template

I am currently developing an integration with a payment service. The payment service has provided me with a form that includes a script tag. I would like to insert this form, including the script tag, into my component template. However, Vue does not allo ...

deleting the selected list item with JavaScript

Currently, I am tackling a todo list project but facing a challenge in finding a vanilla Javascript solution to remove a list item once it has been clicked. Adding user input as list items was relatively simple, but I have come to realize that this specif ...

What is preventing me from updating one dropdown list based on the selection made in another dropdown list?

I have a situation on a classic asp page where there are two dropdown lists: <select id="ddlState" name="ddlState" runat="server"> <option value="KY">Kentucky</option> <option value="IN" ...

Adjust the size of an element using the jQuery library's knob.js

There is a page Once the button is pressed, a circle element from the library jquery.knob.js appears. I am trying to change the size of the circle and have written this code: <div style="float:left; width:255px; height:155px"> <input ...

Simultaneously sending requests with REST API and Ajax

Is it considered problematic to send multiple ajax requests simultaneously to various endpoints of a REST API that ultimately end up affecting the same resource? Please note: each endpoint will be responsible for modifying different properties. For insta ...

Express is having trouble providing data to React

Currently, I am delving into mastering the realms of React and Express. My ongoing project involves crafting a learning application that fetches data from MySQL and renders it visually for analysis. To kickstart this endeavor, I set up a basic express ser ...

Should I use CodeBehind or CodeFile in ASP.Net 3.5/4.0?

After reviewing a previous post on Stack Overflow about the difference between CodeFile and CodeBehind (CodeFile vs CodeBehind), I am still unsure about which one to use. Although it seems that CodeFile is the newer and recommended option, it is interest ...

Developing ASP.NET web services in C# requires strong understanding of IRepository and effective communication

My application includes an IRepository interface that is linked using Ninject and InRequestScope. The binding for the Repository is as follows: kernel.Bind<IRepository>().To<DefaultRepository>().InRequestScope().WithConstructorArgument("dbCon ...

Is there a way to maintain the original indexing of a list after users have selected a filtered item in React using Webpack?

After implementing a filter (filteredCat) for my catalogue, the visual display works as expected. One issue I am facing is that even though the items are filtered, the logged index does not correspond to the new filtered list but instead reflects the inde ...

Error occurs when attempting to filter data through input text pasting in Angular

Currently, I am working on a web application that utilizes the Angular framework and angularfire2. The issue I am encountering is related to the data filter functionality not functioning correctly when pasting copied text. Interestingly, it works perfectly ...

Can dates in the form of a String array be transmitted from the server to the client?

Struggling to send a String array from the server side to the client using Nodejs and Pug. Encounter errors like "SyntaxError: expected expression, got '&'" or "SyntaxError: identifier starts immediately after numeric literal". Server runs o ...

Difficulty with ASP.net C# calculating component

Having trouble with the calculation part of my asp project on a specific page. I've been trying to identify the issue, but I'm stuck. Any assistance would be greatly appreciated. https://i.sstatic.net/SvkZr.png https://i.sstatic.net/o8cXf.png Fo ...

Crafting a dynamic bar chart with recharts for your data visualization

Is it possible to set a custom bar range in the Bar Chart made with recharts? Can we define specific start and end positions for the bars? For instance, if I have two values startPos and endPos, is there a way to create a Bar that starts at startPos and e ...