0

So I am trying to get a series of json results to present itself within a div tag using innerHTML.

   <script>
    $(document).ready(function() {
        var html2 = '';
        var thread_id = '';
        var created_thread_ids = new Array(123123, 12312312, 1231222);
        for (var i in created_thread_ids)
        {
            thread_id = created_thread_ids[i];
            $.getJSON(ravenUrl + '/docs/threads/' + thread_id, function(thread){
                html2 += '<div class="result">' + thread.Title + thread_id + '</div>';
                document.getElementById("showit").innerHTML = html2;
            });
        }
    });
    </script>

    <div id="showit"></div>

My problem is that the variable thread.Title works perfectly but the variable thread_id only works in the first time when it goes to the url and finds the right url but the second time it shows the same id after every thread. Like this:

<div id="showit">
<div class="result">This is the first title123123</div>
<div class="result">This is the second title123123</div>
<div class="result">This is the third title123123</div>
</div>

3 Answers 3

1

The callback function you pass to $.getJSON method is a closure, and as that method is async, will work with the value of thread_id when it will be executed. That's a common gotcha, and has several workarounds, most common is using a wrapper function:

for (var i in created_thread_ids)
  (function(i){
  ...
  var thread_id = created_thread_ids[i];
  $.getJSON(ravenUrl + '/docs/threads/' + thread_id, function(thread){
    html2 += '<div class="result">' + thread.Title + thread_id + '</div>';
    document.getElementById("showit").innerHTML = html2;
  });
  ...
  }(i));
}

... or just ...

for (var i in created_thread_ids) {
  var thread_id = created_thread_ids[i];
  $.getJSON(ravenUrl + '/docs/threads/' + thread_id, 
     ( function(thread_id) {
           return function(thread) {
               html2 += '<div class="result">' + thread.Title + thread_id + '</div>';
               document.getElementById("showit").innerHTML = html2;
           };
     }(thread_id) ) 
  );
}

As a sidenote, I'd strongly recommend to replace (for..in) construct with the convenient for(;;) when it's used to walk over an array (or, as alternative, consider using $.each useful jQuery method).

Sign up to request clarification or add additional context in comments.

Comments

0

Your created_thread_ids is a string, and it should be an array.

change

var created_thread_ids = '123123, 12312312, 1231222';

to

var created_thread_ids = new Array(123123, 12312312, 1231222);

1 Comment

I really appreciate your answer but it actually is an array, it is gathered earlier in the code and when I tried to simplify it here on stackoverflow I accidently made it a string in the code example. Sorry! I assure you that it is the only thing that is changed from the real code.
0

First, as Muth said you should use array and not the string

var createdThreadIds = [123123, 12312312, 1231222]

Second, your code async and you do not know how callbacks from your ajax calls, and they can be all mixed up.

Third, I would recommend you to use Deffered object returned by ajax calls and update DOM only when all calls are performed.

1 Comment

I understand why it wouldn't work thanks to your pointers but I don't really get how to get it to actually work...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.