0

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?

3 Answers 3

4

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>

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

Comments

2

Try this with using join function :

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

Documentation here

Comments

0

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;

1 Comment

innerHTML is a property, not a function.

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.