0

I have below code, I want to add this inside a parent div

<div style="width: 100%; overflow: hidden;">
<div style="width: 10%; float: left;"> <div><img src="assets/img/abc.png" style="height: 35;margin-left: 10; margin-right: 5px;"/></div> <div id="toro" style="color: black">  Hi </div></div>
<div style="margin-left: 25%;"><div style="margin-top: 7;
font-weight: bold; color: black">  <b>hie</b></div> <div style="color: black" >  Hello  </div><div style="float: right" id="time">time</div></div>

Ritzor
  • 665
  • 7
  • 26
  • Possible duplicate of [Add/remove HTML inside div using JavaScript](http://stackoverflow.com/questions/17650776/add-remove-html-inside-div-using-javascript) – Sᴀᴍ Onᴇᴌᴀ Dec 24 '16 at 07:06
  • Please include the code used to create the HTML, as we could probably help you refactor that code. – Jhecht Dec 24 '16 at 07:34

1 Answers1

1

Example to add <div>

It's already answered here Add/remove HTML inside div using JavaScript

var jsonArr = [{
    'id': 1,
    'name': 'RAJ'
  }, {
    'id': 2,
    'name': 'RAY'
  }, {
    'id': 3,
    'name': 'RAM'
  }];

  function addDiv() {
    for (var i = 0; i < jsonArr.length; i++) {
      var div = document.createElement('div');
      div.innerHTML = '<div style="width: 100%; overflow: hidden;">' +
        '<div style="width: 10%; float: left;">' +
        '<div><img src="assets/img/abc.png" style="height: 35;margin-left: 10; margin-right: 5px;"/></div> <div id="toro" style="color: black">' + jsonArr[i].id + '</div>' +
        '</div>' +
        '<div style="margin-left: 25%;">' +
        '<div style="margin-top: 7;font-weight: bold; color: black">  <b>hie</b></div> <div style="color: black" >' + jsonArr[i].name + '</div><div style="float: right" id="time">'+new Date()+'</div></div>';

      document.getElementById('parent').appendChild(div);
    }
  }
<div id="parent">Parent div
</div>
<button onClick="addDiv()">Add Div</button>
Community
  • 1
  • 1
Naghaveer R
  • 2,890
  • 4
  • 30
  • 52