Solution 1 :

Why you are not try using css? I think the below css code will be enough for u

divElem.style.marginBottom = "50px";

Solution 2 :

You can make a template, load your data into it then insert that to a new element.

const root = document.querySelector(".root");

const objs = [
  {title: "Foo1", rating: "bar1", copied_sold: "baz1"},
  {title: "Foo2", rating: "bar2", copied_sold: "baz2"}
];

objs.forEach(obj => {
  const newDiv = document.createElement("div");
  const template = getElementTemplate(obj.title, obj.rating, obj.copied_sold);
  
  newDiv.innerHTML = template;
  root.appendChild(newDiv);
});
          
function getElementTemplate (title, rating, copied_sold) {
  return `
    Title: ${title}<br/>
    Rating: ${rating}<br/>
    Copies_Sold: ${copied_sold}<br/>
    <br/>
  `;
}
<div class="root"></div>

With the correct HTML elements and some CSS you can also do this without the use of a <br/>

const root = document.querySelector(".root");

const objs = [
  {title: "Foo1", rating: "bar1", copied_sold: "baz1"},
  {title: "Foo2", rating: "bar2", copied_sold: "baz2"}
];

objs.forEach(obj => {
  const newDiv = document.createElement("div");
  const template = getElementTemplate(obj.title, obj.rating, obj.copied_sold);
  
  newDiv.innerHTML = template;
  root.appendChild(newDiv);
});
          
function getElementTemplate (title, rating, copied_sold) {
  return `
    <ul>
      <li>Title: ${title}</li>
      <li>Rating: ${rating}</li>
      <li>Copies_Sold: ${copied_sold}</li>
    </ul>
  `;
}
ul {
  list-style-type: none;
  padding: 0;
}
<div class="root"></div>

Solution 3 :

First suggestion:
Move the var divElem = document.getElementById('div_id'); out of the for loop.

Second suggestion:
How about creating the HTML as a templated string?

  var obj = res[i];
  divElem.innerHTML += 
    `Title: ${obj.title}<br>`+
    `Rating: ${obj.rating}<br>`+
    `Copies_Sold: ${obj.copied_sold}<br>`+
    `<br>`;

Problem :

So I had to make a website where it make an http request and obtained an array of json data and the format was supposed to look like this:

Title: data1
Rating: data2
Copies_Sold: data3

Title: data4
Rating: data5
Copies_Sold: data6

Eventually I came up with this:

 var res = JSON.parse(xhr.responseText);
          for (var i = 0; i < res.length; i++) {
            var obj = res[i];
            var divElem = document.getElementById('div_id');
            var brElem = document.createElement('br');
            var title = document.createTextNode('ID: '+obj.title+' ');
            var rating = document.createTextNode('First Name: '+obj.rating+' ');
            var sold = document.createTextNode('Last Name: '+obj.copied_sold+' ');
            divElem.appendChild(document.createElement('br'));
            divElem.appendChild(title);
            divElem.appendChild(document.createElement('br'));
            divElem.appendChild(rating);
            divElem.appendChild(document.createElement('br'));
            divElem.appendChild(sold);
            divElem.appendChild(document.createElement('br'));
          }

However I believe it kinda eliminates the DRY (Do not Repeat Yourself) philosophy. Is there a better way to do this?

Comments

Comment posted by Pointy

Make the elements themselves

Comment posted by epascarello

It iooks like a definition list, use that and use CSS to make it look that way.

Comment posted by Anurag Hazra

Just a FYI, make sure to sanitize the inputs or use

Comment posted by Azzarian

I really apreciate the answer but personaly I am not a big fan of innerHTML since as described below it can cause XSS vulnerability,

Comment posted by iAmOren

Okay. Well, you can write a function

By