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 6877963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:39:53+00:00 2026-05-27T04:39:53+00:00

I have a webpage with different elements (a list of links and two select

  • 0

I have a webpage with different elements (a list of links and two select boxes) connected between them. Clicking on them may affect one of the other element and all of their values contribuite to update a value to show on the page.

So, the code is this:

$(document).ready(function() {

    var someVar = '';

    $("select#size").bind('change', function() {
        someVar = $(this).val();
        console.log('first');
    });

    my_change();
    console.log('second' + someVar);
});

function my_change() {

    $.getJSON("photos/change_product", {json_stuff}, function(data) {
        var options = [];
        for (var i = 0; i < data.length; i++)  {
            options.push('<option value="' + data[i].id + '">' + data[i].label + '</option>');
        }
        $("select#size").trigger('change');
        $("select#options").html(options.join('')).trigger('change');
    })
};

};

When I load the page the my_change function is called. It does some stuff and then triggers a change event on a select-box. I need to update a value using what’s inside this select box and only then let the execution to proceed. So what I need this code to do would be to print ‘first’, and then ‘second’ with the value of the variable. What actually happen is that it prints ‘second’ ‘first’.

I think it’s because I’m doing asynchronous calls. What can I do?

  • 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-05-27T04:39:54+00:00Added an answer on May 27, 2026 at 4:39 am

    The ‘second’ console.log() is being called first since the asynchronous $.getJSON() call waits for the response from the server before firing its callback function. You could save the jqXHR object to a variable and then use that to run your ‘second’ consone.log() with $.when():

    $(function() {
    
        var someVar = '';
    
        $("#size").on('change', function() {//on() is the same as bind() here
            someVar = $(this).val();
            console.log('first');
        });
    
        //save the jQuery XHR object from your $.getJSON request
        var jqXHR = my_change();
    
        //when the above jQuery XHR object resolves, it will fire the second console.log
        $.when(jqXHR).then(function () {
            console.log('second' + someVar);
        });
    });
    
    function my_change() {
    
        //here we return the jQuery XHR object for the $.getJSON request so we can run code once it resolves
        return $.getJSON("photos/change_product", {json_stuff}, function(data) {
            var options = [];
            for (var i = 0; i < data.length; i++)  {
                options.push('<option value="' + data[i].id + '">' + data[i].label + '</option>');
            }
            $("#size").trigger('change');
            $("#options").html(options.join('')).trigger('change');
        })
    };
    

    Here is documentation for $.when(): http://api.jquery.com/jquery.when

    A quick side-note: it is generally slower to add a tag-type to a selector, especially when you are selecting IDs as that is already a very fast method of selecting elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a navigation bar on my webpage with links to different pages. Is
I have two different div elements on two different webpages that use two different
I have a webpage that implements a set of tabs each showing different content.
I have a webpage, on which I am using Ajax to replace different divs
I have a webpage where Firefox 2 displays the font certain, really specific elements,
I have a webpage with multiple DIV elements. If i use the jquery function
I have a webpage that uses different languages stored in localStorage, and on the
I need to have multiple forms in the same webpage, all of them POSTing
I have got a list of many web page URLs.. and all of them
I have an web page where form input elements are in different places and

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.