Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:55:43+00:00 2026-06-14T00:55:43+00:00

I am trying to attach the same jQuery and JSON ASP.NET MVC3 Method to

  • 0

I am trying to attach the same jQuery and JSON ASP.NET MVC3 Method to two different <select> elements:

<select id="StartYear">
<option value="">Select Year</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
</select>

<select id="StartMonth">
<option value="">Select Month</option>
</select>

Same applies for <select> elements with ids “EndYear” and “EndMonth”.

I want to attach to both “Start Year” and “End Year” the same jQuery function:

$('[id$="Year"]').change(function () {
        var selectedYear = $(this).val();
        var selectedId = $(this).attr('id')
        if (selectedYear != null && selectedYear != '') {
            $.getJSON('@Url.Action("Months")', { year: selectedYear }, function (months) {
                var monthsSelect = $('#StartMonth');
                if (selectedId == 'EndYear') {
                    monthsSelect = $('#EndMonth');
                }
                monthsSelect.empty();
                daysSelect.prepend("<option text='-- select month --' value='' selected='selected'></option>");
                $.each(months, function (index, month) {
                    monthsSelect.append($('<option/>', {
                        value: month.value,
                        text: month.text
                    }));
                });
            });
        }
    });

However, this code does not work. I debugged it with Bugzilla and I found out that this condition (selectedId == 'EndYear') breaks the function. The variable selectedId is in the closure scope but I don’t know how to bring it inside the function scope. Otherwise I don’t know what it cna be the cause.
Keep in mind that without that condition the code was working (of course just with #StartMonth)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:55:45+00:00Added an answer on June 14, 2026 at 12:55 am

    If I correctly understand your problem the variable selectedId can do be accessed inside of success handler of $.getJSON, but the value of the variable can be already changed by another change event.

    For example if two change events will be stated in short time interval from for example “#EndYear” and “#StartYear” then could be the following

    1. change events for “#EndYear” will be triggered
    2. selectedId variable will be set to “EndYear”
    3. first $.getJSON will be started
    4. change events for “#StartYear” will be triggered
    5. selectedId variable will be set to “StartYear”
    6. second $.getJSON will be started
    7. first $.getJSON will be finished and the value of selectedId variable will be “StartYear” inside of the success handler
    8. second $.getJSON will be finished and the value of selectedId variable will be “StartYear” inside of the success handler

    So I think that you need just need to have different instances of selectedId variable to use the value which was before the $.getJSON took place. You can implement that in different way. It seems the most native for me to replace $.getJSON to $.ajax and to use context parameter of $.ajax. The code could be about the following

    $('[id$="Year"]').change(function () {
        var selectedYear = $(this).val();
        var selectedId = $(this).attr('id')
        if (selectedYear != null && selectedYear != '') {
            //$.getJSON('@Url.Action("Months")', {year: selectedYear}, function(months){
            $.ajax({
                url: '@Url.Action("Months")',
                dataType: 'json',
                data: { year: selectedYear },
                context: {selectedId: selectedId},
                success: function (months) {
                    var selectedId = this.selectedId; // get from context
                    ....
                }
            });
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is in continuation of two ongoing problems I'm facing: Problems trying to attach
I am trying to attach a JQuery color picker plug-in to a textbox. The
I am trying to attach an image using jQuery but have gotten a strange
I'm trying to attach a single NetStream Object to two separate Video objects instead
I am trying to attach the Netbeans profiler to my Java project but it
I am trying to attach onBlur and onFocus handler to a SSN input field.
I'm trying to attach my database on a server that is running SQL2005 and
I'm trying to attach < Previous and Next > links to a jQueryUI datepicker
I am trying to attach a function-object to be called on destruction of a
I am trying to attach a video in my mms application. But the application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.