2

I'm working on a wordpress editor for my blog, and need to be able to make sure that the function doGET() is finished before I click the save button with javascript or jquery. The doGET function might take a tad of time as I'm in a country with incredible slow internet connection...

$('#xmlbutton').click(function() {

    doGET();

    document.getElementById("save-post").click();  

})
2famous.TV
  • 460
  • 1
  • 6
  • 23
  • possible duplicate of [jQuery: wait for function to complete to continue processing?](http://stackoverflow.com/questions/1455870/jquery-wait-for-function-to-complete-to-continue-processing) – Denys Séguret Jul 19 '13 at 12:04

1 Answers1

3

You have to put...

document.getElementById("save-post").click();

in the callback of doGET() since it's an asynchronous operation.

Since I don't have all your code I made a little example.

$('#xmlbutton').on('click', function(){
  $.get('/post/edit', {"id":"12"}, function(result){
    $('#save-post').trigger('click');
  })
});

PS: Instead of $('#save-post').trigger('click'); you should probably do $('#save-post-buttons-form').trigger('submit');.

Bill Criswell
  • 32,161
  • 7
  • 75
  • 66