0

i have a results object that include some data like Creation_date, approval_date,.... . i put the results in loop in javascript and create nested div. but when i debug it , those div aren't nested,unfortunately each one closed after created. any help?

    var Item=document.getElementById('myItems');
    Item.innerHTML="";
    for(var i=0;i<myItem.results.length;i++){

        Item.innerHTML +='<div class="row">';
            Item.innerHTML +='<div class="col-md-12">';
                Item.innerHTML +='<img src="'+myItem.results[i].images+'"/>';
            Item.innerHTML +='</div>';
        Item.innerHTML +='</div>';
        Item.innerHTML +='<div class="row">';
            Item.innerHTML +='<div class="col-md-4">';
                Item.innerHTML +='Created on:'+myItem.results[i].creation_date;
            Item.innerHTML +='</div>';
            Item.innerHTML +='<div class="col-md-4">';
                Item.innerHTML +='incident_date:'+myItem.results[i].item_date;
            Item.innerHTML +='</div>';
            Item.innerHTML += '<div class="col-md-4">';
                Item.innerHTML +='approval_date:'+myItem.results[i].approved_date;
            Item.innerHTML +='</div>';
        Item.innerHTML +='</div>';
    }
1
  • Try using appendChild rather than innerHTML. Commented Feb 9, 2016 at 17:07

3 Answers 3

1

You're assigning to .innerHTML. Every time you do that, the dom is rebuild. JS and the DOM engine have no idea you're going to be adding more stuff again, so it tries to rebuild the DOM tree with VALID html, fixing any errors you introduced with the one line added.

e.g.

item.innerHTML = '<div>'; 

actually produces <div></div> in the tree, because you're not allowed to build an invalid tree.

Don't do repeated innerHTML assignment. Build your html in a string, then assign the string ONCE

e.g.

foo = '<div>';
foo += 'hi mom!';
foo += '</div>';

Item.innerHTML += foo;

Not only does this let you build up your html properly, it's also more efficient, and only rebuilds the tree ONCE, instead of each time you modified innerhtml.

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

Comments

0

Put all html in buffer and insert it after for loop.

Since you are directly assigning innerHTML DOM is contineuosly re-building in for loop.

var Item = document.getElementById('myItems');
var buffer = '';
for (var i = 0; i < myItem.results.length; i++) {

  buffer += '<div class="row">';
  buffer += '<div class="col-md-12">';
  buffer += '<img src="' + myItem.results[i].images + '"/>';
  buffer += '</div>';
  buffer += '</div>';
  buffer += '<div class="row">';
  buffer += '<div class="col-md-4">';
  buffer += 'Created on:' + myItem.results[i].creation_date;
  buffer += '</div>';
  buffer += '<div class="col-md-4">';
  buffer += 'incident_date:' + myItem.results[i].item_date;
  buffer += '</div>';
  buffer += '<div class="col-md-4">';
  buffer += 'approval_date:' + myItem.results[i].approved_date;
  buffer += '</div>';
  buffer += '</div>';
}

Item.innerHTML = buffer;

Comments

0

I don't see why you just don't use jQuery .append() http://api.jquery.com/append/

2 Comments

jQuery isn't javascript?
I know that - but it makes for a much simpler solution to what he's trying to achieve than the way he's currently doing it.

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.