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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:29:24+00:00 2026-05-29T05:29:24+00:00

On this code: $.each(divList, function(){ var dropdown = ”; $.get(ajax.php,{‘some’:’params’, ‘other’:’params’},function(msg){ dropdown = msg;

  • 0

On this code:

$.each(divList, function(){ 
    var dropdown = '';
    $.get("ajax.php",{'some':'params', 'other':'params'},function(msg){
        dropdown = msg;
        console.log( dropdown );
    });
    console.log( dropdown );
    $(dropdown).appendTo($(this));
});

…the first log shows dropdown variable holds the correct text returned from the ajax, the second one is empty (and the appendTo does nothing).

Is there a way to make this works? Is there some dependency with the browser used ?

  • 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-29T05:29:25+00:00Added an answer on May 29, 2026 at 5:29 am

    $.get() is asynchronous: it returns very quickly, performs its work in the background and signals completion by invoking the callback function you supply.

    Since your second call to console.log() and your call to appendTo() are outside the callback, they will run too soon, before $.get() completes and dropdown is set.

    The usual pattern is to put the call to appendTo() inside the callback function:

    $.each(divList, function() {
        $.ajax({
            url: "ajax.php",
            data: {
                // your parameters...
            },
            context: this,
            success: function(msg) {
                console.log(msg);
                $(msg).appendTo(this);
            }
        });
    });
    

    EDIT: If you want to access the original this reference from your callback function, you can pass it in the context option to $.ajax(), as the code above does.

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

Sidebar

Related Questions

I have this code: $.getJSON(Featured/getEvents, function(data){ $.each(data.events, function(i,event){ var title = event.title.substr(0,20); $(#title-+i).text(Text); if
I have this js code: $(#startSearch).live(click, function(event) { $(input:checkbox[name='searchId']:checked).each(function() { var searchId = $(this).val();
I have this code: for each(var tool in tools){ tool.addEventListener(MouseEvent.MOUSE_DOWN, function(){ trace(tool); //Always the
I am using the following code: $.each($('.friendName'), function(){ if($(this).val() != ''){ var idName =
I have this code which will include template.php file from inside each of these
I used a code: jQuery.fn.MyFunction = function(){ return this.each(function() { attributes = test; return
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Given the following jquery code.. jQuery.fn.sliding = function () { return this.each(function () {
i am fetching data using following code _.each(cardIds, function(id){ var mdc = new MdlCard({cardId:
I have this code: $.each(properties, function(i, key) { obj.css({'-webkit-border-' + key + '-radius': value+'px',

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.