0

I have a button in my html page and when i click on that following function is called:

$("#rlsLckButton").click(function () {
    var userIds = '';
    var messages = [];
    for (i = 1; i < counter; i++) {
        userIds = $('#textbox' + i).val();
        if (userIds != " ") {
            $.getJSON('execLock.action', {
                RsUserIds: userIds
            }, function (json) {
                for (var i = 0; i < json.returnMessages.length; i++) {
                    messages.push(json.returnMessages[i]);
                }
            });
        }
    }    
    $("#TextBox").empty();
    for (var i = 0; i < messages.length; i++) {
        $("#TextBox").append('<p>messages[i]</p>');
    }
});

So what is happening is it is calling $getJson function then its coming out of it and executing other code

$("#TextBox").empty();
for (var i = 0; i < messages.length; i++) {
    $("#TextBox").append('<p>messages[i]</p>');
}

Then again its calling same getJSON function and this time its populating the values but not calling next followed code and coming out of the function. Not sure why its happening .Please suggest ! Help is appreciated.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
abhinav singh
  • 856
  • 6
  • 23
  • 45
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Arun P Johny Dec 12 '13 at 06:57
  • where is counter defined ? – A.T. Dec 12 '13 at 06:58
  • Have you tried putting the second bit of code inside the getJSON callback? – tewathia Dec 12 '13 at 06:58
  • Try `$("#rlsLckButton").click(function () { var userIds; $("#TextBox").empty(); for (i = 1; i <= counter; i++) { userIds = $.trim($('#textbox' + i).val()); if (userIds != "") { $.getJSON('execLock.action', { RsUserIds: userIds }, function (json) { var html = $.map(json.returnMessages, function (message) { return '

    ' + message + '

    ' }).join('') $("#TextBox").append(html); }); } } });`
    – Arun P Johny Dec 12 '13 at 07:08
  • @Arun Don't post large sections of code into the comments. That's next to useless. – Tomalak Dec 12 '13 at 08:18
  • @Tomalak I'm not sure whether it will solve the problem... also didn't had time to explain it... so it was a safer bet from downvotes – Arun P Johny Dec 12 '13 at 08:19
  • Simple: If your answer is good enough to post it, post it as a real answer. If you don't think it's good enough, don't post it at all. Posting it as a comment for fear of down-votes is counter-productive on all levels. – Tomalak Dec 12 '13 at 08:23
  • @Tomalak I used to do that... but then the advice I got was if it is not possible to explain the solution then don't answer.... Again I thought it will be better to give the OP a chance to solve the problem if I can add it as a comment – Arun P Johny Dec 12 '13 at 08:35
  • @ArunPJohny: Didn't worked :( – abhinav singh Dec 12 '13 at 17:14
  • @ArunPJohny:It worked after using $.ajaxSetup({async: false});... Now if i want to show the div contents one by one how can i do it ? I mean show first message then second then third and so on . Right now its showing all the messages at once... Please suggest! – abhinav singh Dec 13 '13 at 03:57
  • what do you mean by one by one? never use aysnc: false... if you want to keep the order then you can use $.when() addionaly – Arun P Johny Dec 13 '13 at 04:07

0 Answers0