e.iluf
e.iluf

Reputation: 1659

how to change array output

I have an array "DArr" that I want to display all the content. I use this code to set the input to the html id

 document.getElementById("output").innerHTML = [DArr];

the output as expected is in this format

1,2,3,4,5

what I want is have the output in this format

1

2

3

4

5

how can I implement this?

Upvotes: 0

Views: 62

Answers (3)

Chris
Chris

Reputation: 3338

innerHTML can accept a string of HTML. You can combine this with Array.join to combine your output with <br /> tags to create the desired effect:

var html = DArr.join('<br />');
// 1<br />2<br />3<br />4<br />5

document.getElementById("output").innerHTML = html;

Upvotes: 0

Intervalia
Intervalia

Reputation: 10945

Try this:

var output = document.getElementById("output");
var dArr = [1,2,3,4,5];

dArr.forEach(
  function(val) {
    var newEl = document.createElement('div');
    newEl.textContent = val;
    output.appendChild(newEl);
  }
);
<div id="output"></div>

Or try this:

dArr = [2,3,4,5,6];

document.getElementById('output').innerHTML = dArr.join('<br/>');
<div id="output"></div>

Upvotes: 4

GGO
GGO

Reputation: 2748

Try this with using join function :

document.getElementById("output").innerHTML = DArr.join("<br />");

Documentation here

Upvotes: 2

Related Questions