1

I have a simple bootstrap page I need to load JSON data into it:

This is my script:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
        var tweets = JSON.parse(xhr.responseText);  

        var tweet = '   <div class="col-xs-12 col-sm-6 col-md-8 col-md-offset-3">';
        var name = ' <h4 class="card-title">';
        var photo = '    <div class="col-md-4 col-md-offset-3"><div class="col-md-4 ">';

        for (var i = 0; i < tweets.length; i += 1) {

            name += tweets[i].name;
            name += '</h4> </div>';

            tweet += ' <div class="jumbotron">';
            tweet += '  <p  class="card-text">';
            tweet += tweets[i].tweet;
            tweet += "<br>";
            tweet += "</p></div></div>";

            photo += '  <img class="img-responsive img-circle" src="user/';
            photo += tweets[i].activation;
            photo += '/';
            photo += tweets[i].pic;
            photo += '"></div></div>';

            document.getElementById('photo').innerHTML = photo;
            document.getElementById('name').innerHTML = name;
            document.getElementById('tweet').innerHTML = tweet;

            // close all html tags   
        }
    }
};
xhr.open('GET', 'empdata.json');
xhr.send();

And this is my HTML:

<div class="container">
    <div class="card">
        <div class="card-header">    
            <div class="row">
                <div id="photo">
                    <!-- here  where the pic goes ! -->
                </div>
                <div id="name">
                    <!-- here  where the name goes ! -->
                </div>
            </div>    
            <div class="card-block row">
                <div id="tweet">
                    <!-- here  where the tweet goes ! -->
                </div>
            </div>
        </div>
    </div>
</div>
</div>

<div class="card-block row">    
    <div id="tweet"> </div>
</div>

It loops correctly through all JSON data, but it displays all the pictures first then the names and finally the tweets separately. think the problem is related to the mechanism I follow in the loop.

6
  • what are you trying to accomplish? Commented Aug 31, 2016 at 13:22
  • you need to append the html outside of the loop Commented Aug 31, 2016 at 13:23
  • or do document.getElementById('photo').innerHTML += photo; Commented Aug 31, 2016 at 13:23
  • Would be so easy and nice with an ng-repeat Commented Aug 31, 2016 at 13:25
  • to display the tweet and its corresponding data(name and pic), but instead javascript loops through all pictures then name and then tweet all separately Commented Aug 31, 2016 at 13:26

2 Answers 2

1

I think you mean to do this. It can be much simpler but without changing your html I would do this:

var container = document.querySelector(".container");
for (var i=0; i<tweets.length; i += 1) {
    var name = ' <h4 class="card-title">';
    name += tweets[i].name;
    name += '</h4> </div>';
    var tweet = '<div class="col-xs-12 col-sm-6 col-md-8 col-md-offset-3">'
    tweet += ' <div class="jumbotron">';
    tweet += '  <p  class="card-text">';
    tweet += tweets[i].tweet;
    tweet += "<br>";
    tweet += "</p></div></div>";
    var photo ='<div class="col-md-4 col-md-offset-3"><div class="col-md-4 ">' 
    photo += '  <img class="img-responsive img-circle" src="user/';
    photo += tweets[i].activation;
    photo += '/';
    photo += tweets[i].pic;
    photo += '"></div></div>';

    container.innerHTML+='<div class="card"><div class="card-header">'+
    photo+name+tweet+'</div></div>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Better move the var container = document.querySelector(".container"); outside the loop - also if you need, concatenate the html to a var and after the loop add that var to the container
0

EDITED Try set innerText outside from the loop

for (var i=0; i<tweets.length; i += 1) {

        name += tweets[i].name;
        name += '</h4> </div>';
              ...
        photo += tweets[i].pic;
        photo += '"></div></div>';

    }
        document.getElementById('name').innerHTML = name;
        document.getElementById('photo').innerHTML = photo;

Comments

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.