Link to home
Create AccountLog in
Avatar of peter-cooper
peter-cooper

asked on

Run multiple $.get() from single change event

I am looking for the correct way to run more than 1 $.get() event from a single change event. The code I have posted only returns the first call to loadboxDstrsubcat.php?dstrdept.

I would be grateful if someone could point me in the right direction. I know there is the .when option, but I am still quite green to ajax so any help would be mist welcome. Thanks

$(function() {

$("#dstr_dept").chosen({
      width: "260px",
      placeholder_text_single: "Select Some Options"
    }).change(function() {

    $(this).after('<div id="loader"><imgages src="img/loading.gif" alt="loading files" /></div>');
    $.get('loadboxDstrsubcat.php?dstrdept=' + $(this).val(), function(data) {
      $("#box_dstr").html(data);
      $('#loader').slideUp(200, function() {
        $(this).remove();
        $("#box_dstr").trigger("chosen:updated");
      });
    });
      $(this).after('<div id="loader"><imgages src="img/loading.gif" alt="loading files" /></div>');
      $.get('loadboxAdrDstrsubcat.php?dstraddr=' + $(this).val(), function(data) {
      $("#dstr_address").html(data);
      $('#loader').slideUp(200, function() {
        $(this).remove();
        $("#dstr_address").trigger("chosen:updated");
      });
    });
  });
});

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Not sure if this will work, but you can try something like the following for a asynchronous result(just pseudo code here):
$.when(
  $.get('your stuff here', function() {
	//your stuff here
  });
).then(
  $.when(
	  $.get('your stuff here', function() {
		//your stuff here
	  });
  ).then(
    //and so forth
  );
);

Open in new window


If this is a bit nasty, you can try the following - also asynchronous:
$.get( "example1.php", function() {
  alert( "success1" );
}).done(function() {
    $.get( "example2.php", function() {
		alert( "success2" );
	}).done(function() {
		//and so forth
	});
});

Open in new window


Haven't tested these actually, just guessing :)
Avatar of peter-cooper
peter-cooper

ASKER

@leakim
Works a treat thanks. Only question I have is, is it ok to have the loader named the way it is? ie, same id? thx
Thank you for your help