title: creating button in for loop using javascript
body: <p>I send the parameters to the server and it responses me with an object of array.
  I get it and use the <code>JSON.stringify</code> function and then <code>JSON.parse</code>.
  When I assign the button in the body it works properly though it isn't placed correctly in the html. If I assign to the iteration <code>id</code> it has an error.
  What should I do to handle the problem and have a click event to delete the current item and delete it.
  Here is my code:</p>
  
  <pre><code>&lt;script&gt;
  var param={user_id: localStorage.getItem('user_id')};
  getmyItems(param,function(data){
      var mtItem=JSON.stringify(data);
      myItem=JSON.parse(mtItem);
      console.log(myItem);
      var Item=document.getElementById('myItems');
      var buffer="";
      for(var i=0;i&lt;myItem.results.length;i++){
  
          buffer +='&lt;div class="row"&gt;';
              buffer +='&lt;div class="col-md-12"&gt;';
                  buffer +='&lt;img src="'+myItem.results[i].images+'"/&gt;';
              buffer +='&lt;/div&gt;';
          buffer +='&lt;/div&gt;';
          buffer +='&lt;div class="row"&gt;';
              buffer +='&lt;div class="col-md-4"&gt;';
                  buffer +='Created on:'+myItem.results[i].creation_date;
              buffer +='&lt;/div&gt;';
              buffer +='&lt;div class="col-md-4"&gt;';
                  buffer +='incident_date:'+myItem.results[i].item_date;
              buffer +='&lt;/div&gt;';
              buffer += '&lt;div class="col-md-4"&gt;';
                  buffer +='approval_date:'+myItem.results[i].approved_date;
              buffer +='&lt;/div&gt;';
          buffer +='&lt;/div&gt;';
          buffer+='&lt;div class="row"&gt;';
              buffer+='&lt;div class="col-md-12"&gt;';
                  buffer+='Title: '+myItem.results[i].title;
              buffer+='&lt;/div&gt;';
          buffer+='&lt;/div&gt;';
          buffer+='&lt;div class="row" id="'+i+'"&gt;';
              var btn=document.createElement('input');
              btn.setAttribute('type','button');
              btn.setAttribute('class','btn btn-danger');
              btn.value="Delete";
              btn.onclick=(function (i){
                  return function(){
                      doDelete(myItem.results[i].item_id);
                  };
                  })(i);
  
              document.getElementById('i').appendChild(btn);
  
          buffer+='&lt;/div&gt;&lt;hr/&gt;';
      }
      Item.innerHTML=buffer;
  });
  
  function doDelete(s){
          var param={
              user_id: localStorage.getItem('user_id'),
              session: sessionStorage.getItem('session'),
              item_id: s
          };
          deleteItem(param, function(data){
              alert(JSON.stringify(data));
          });
      }
     &lt;/script&gt; 
  </code></pre>