The JavaScript class failed to work properly within the asp.net framework

I have successfully implemented a JavaScript function on my aspx page. Here is the code snippet:

<script type="text/javascript">
        $(document).ready(function () {
            console.log("ready!");
            var options = [];
            var year = 1950;
            for (var i = 0; i <= 70; i++) {
                if (i == 0) {
                    options[i] = '<option value="0" selected="selected"> Choose Year</option>';
                }
                else {
                    options[i] = '<option value="' + (parseInt(1950) + parseInt(i - 1)) + '">' + (parseInt(1950) + parseInt(i - 1)) + '</option>';
                }
            }
            $('#yearid').get(0).innerHTML = options.join('');
        });
        $("#yearid").change(function () {
            var selectedValue = $("#yearid option:selected").val();
            $("#yearval").val(selectedValue);
        });         
    </script>

I want this function to be executed within the following HTML structure:

 <div class="col-md-4 form-group">
                <span>Year : </span>
                <select id="yearid" class="form-control" runat="server">

                </select>

                <input id="yearval" type="hidden"  runat="server"/>
            </div>

Once the code runs, the JavaScript function mentioned above should populate the " Choose Year" value inside the select element as depicted above.

Despite my efforts, the code does not seem to affect the select element. I would appreciate any assistance. Thank you.

Answer №1

It appears that your code is functioning correctly below. However, it is advisable to verify the presence of the jQuery script to ensure it is included.

$(document).ready(function () {
            console.log("ready!");
            var output = [];
            var yr = 1950;
            for (var i = 0; i <= 70; i++) {
                if (i == 0) {
                    output[i] = '<option value="0" selected="selected"> Choose Year</option>';
                }
                else {
                    output[i] = '<option value="' + (parseInt(1950) + parseInt(i - 1)) + '">' + (parseInt(1950) + parseInt(i - 1)) + '</option>';
                }
            }
            $('#yearid').get(0).innerHTML = output.join('');
        });
        $("#yearid").change(function () {
            var select = $("#yearid option:selected").val();
            $("#yearval").val(select);
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-4 form-group">
                <span>Year : </span>
                <select id="yearid" class="form-control" runat="server">

                </select>

                <input id="yearval" type="hidden"  runat="server"/>
            </div>

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

Extract source, medium, etc. data from __utmz GA with the help of JavaScript

Is it possible to retrieve the campaign, source, and medium that Google Analytics records all at once when the page loads? If so, could I use jQuery to look up the values from __utmz? ...

Issues arise when using ng-repeat in conjunction with ng-click

I am facing some new challenges in my spa project with angularjs. This is the HTML snippet causing issues: <a ng-repeat="friend in chat.friendlist" ng-click="loadChat('{{friend.friend_username}}')" data-toggle="modal" data-target="#chat" d ...

Displaying multiple categories of articles on a single page with Node.js

Seeking a solution to limit the number of posts displayed for each category using a .limit() function, but uncertain on how to proceed. Currently utilizing Mongoose and Express in my code setup. The existing code snippet is outlined below: router.get(&a ...

What is the best way to eliminate a particular item from an array that is nested within the object? (using methods like pop() or any

I am struggling to remove the 'hello5' from the years in myObj. Although I tried using the 'pop' prototype, an error occurred in the browser console displaying: 'Uncaught TypeError: Cannot read property 'type' of undefi ...

Retrieve information from a variety of selected checkboxes

Is there a way to retrieve the values of check boxes that are generated dynamically? @ $db = mysql_connect("abc", "abc", ""); mysql_select_db("abc"); $strSQL = "SELECT * FROM student"; ...

How can we use Cypress to check if we are at the final slide in the presentation?

I am facing a challenge with multiple slideshow files that contain varying numbers of slides. Some have 10 slides, while others have 80 slides. My goal is to test every slide by using the forward and backward arrow buttons for navigation. Currently, I am c ...

What is the best way to test a component that displays its children elements?

I am encountering an issue with testing the functionality of a component I have created. The component is a Button that accepts props such as className, children, and otherProps. export default function Button({ className, children, ...otherProps }) { r ...

Using Django (v3) UserCreationForm, the process of applying Bootstrap4 form-related classes to fields can be simplified

I am currently in the process of creating a registration form. The structure of the form is as follows: class UserSignUpForm(UserCreationForm): username = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', ' ...

Changing a table cell's value when a button is clicked using Angular

I'm working with a bootstrap table that includes a button and a textbox above it. After entering a number in the textbox and clicking the generate button, I need to update the "Bill Amount" column for each row with the value from the textbox. How can ...

Creating an HTML file using PUG in a local environment (using npm and gulp)

Is there a way to automatically generate an HTML file with the same name as my Pug file whenever I save using gulp? I've checked all the documentation on but it only explains how to return Pug content in console... ...

How can I retrieve an item from a JSON array if it is not an array in ASP.NET?

Is there a way to extract the Label, AskPrice, and LastPrice from the JSON data retrieved through the Cryptopia API? The JSON response contains multiple records, and I need to loop through them which requires converting them into an array. However, I enc ...

Using null or undefined for ternary operator formatting in React applications

When styling a component, what should be used if a specific condition is not fulfilled: null, undefined, or something else? For example: errorStyle: { right: locale === Locales.ARABIC ? 0 : null, left: locale !== Locales.ARABIC ? 0 : null, .. ...

Steps to Remove the Displayed Image upon Click

I have a collection of images such as {A:[img1,img2], B:[img1]}. My goal is to remove the array values that correspond to previewed images upon clicking the delete button. Each image is equipped with its own delete button for this purpose. This snippet ...

What is the best way to show JavaScript output with span style?

I have added a translation feature that allows users to switch paragraphs to French when clicked. Each paragraph now has the first word wrapped in a span with a CSS class that enlarges and colors the text. However, I am facing an issue where when switchi ...

Is there a way to transmit a div using Node.js?

Scenario: I am developing a web application that allows users to draw on a canvas, save the drawing as an image, and share it with others using Node.js. Current Progress: Drawing functionality (real-time updates on both clients). Saving the canvas as a ...

Bootstrap datetimepicker is not showing the calendar for selecting a date, making it impossible to choose a specific date

I am facing an issue with integrating the bootstrap datetimepicker with Datatables to filter the data based on selected dates. The problem is that only an input bar is displayed on the page, and there is no calendar or calendar symbol visible. I suspect it ...

Is there a way to prevent pixels from being rendered outside of a designated rectangle in HTML5?

I'm looking to add screen-in-screen functionality to my HTML5 game, and I have an idea for how to approach it: //Function called every frame function draw(){ set a mask rectangle only draw pixels from the current frame that fall within this recta ...

Guide on deactivating the div in angular using ngClass based on a boolean value

displayData = [ { status: 'CLOSED', ack: false }, { status: 'ESCALATED', ack: false }, { status: 'ACK', ack: false }, { status: 'ACK', ack: true }, { status: 'NEW', ack ...

What could be causing the background color not to change on the HTML page with Bootstrap?

Learning html, bootstrap, and css styling can be a confusing endeavor. Sometimes things don't work as expected, like changing the background color. Despite following what seems like a simple solution after googling, the background color remains unchan ...

Easiest method for implementing event emitter in ES6

Here is the current setup for my event emitter: class Table { set onDiscard(cb) { this._onDiscard = cb; } notifyDiscard(s) { if (this._onDiscard) { this._onDiscard(s); } } } Dealing with multiple events using this method can b ...