0

I have a javascript array where, for each iteration, a name is added to the end using the push feature. I'm sending this array to a html file where it will be displayed. Currently displaying this array after all iterations have completed gives:

John, Smith, Paul, Doe

However, I wish the array to be displayed vertically like:

John
Smith
Paul
Doe

In Javascript, is there some way to specify for each name to be entered as a new row in the array? In Matlab you can easily specify which row or column you want data to be stored in the array but I am yet to find a simple approach in Javascript.

Thank you for any help you might be able to give me!

UPDATE: Javascript (simplified):

port.onMessage.addListener(function(msg) {
    var Array = []
    var Names = ["John", "Smith", "Paul", "Doe"]
    for (var p = 0; p < Names.length; p++) {
    Array.push(Names[p])
    }
    document.getElementById('status').textContent = Array;
});

HTML (simplified):

<html>
    <head>
    <script src="Javascript.js"></script>
        <style type="text/css" media="screen">
            body { 
                min-width       : 400px;
                min-height      : 300px;
                text-align      : right;
                background-color: #D0D0D0; }
            #status { 
                font-size       : 10px;
                color           : #0000CC;
                text-align      : left;
                font-family     : "Times New Roman", Times, serif; }
        </style>
    </head>
    <body>
    <div id="status"></div>
    </body>
</html>
7
  • 2
    Presentation is not handled by JavaScript. You can show your array however you want. It all depends on what is the output, like the console, or a webpage. Commented Jul 1, 2015 at 3:00
  • do you mean like array[3] = 'John' ? Commented Jul 1, 2015 at 3:00
  • 2
    The html / dom displays your list, not the JS array representaion. Add <br /> tags between items. Commented Jul 1, 2015 at 3:00
  • Please provide some code so we can help. Commented Jul 1, 2015 at 3:01
  • Yes, guergana, something like that. Commented Jul 1, 2015 at 3:05

3 Answers 3

4

To respect newlines in CSS you can use the white-space property. First join the items with newlines:

var array = ['one', 'two', 'three']
element.textContent = array.join('\n')

Then set the appropriate CSS:

element {
  white-space: pre
}

var p = document.querySelector('p')

var array = ['one', 'two', 'three']

p.textContent = array.join('\n')
p {
  white-space: pre
}
<p></p>

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

1 Comment

Where, within the html file, are the <p> tags placed? In the body? And is the p{} section placed within the <style> section?
2

Try utilizing Array.prototype.forEach to iterate array arr , create text node for each item within arr , append text node with "\n" concatenated to pre element.

var arr = ["John", "Smith", "Paul", "Doe"];
var elem = document.getElementsByTagName("pre")[0];
arr.forEach(function(el, i) {
  elem.appendChild(
    document.createTextNode(el + "\n")
  )
})
<pre></pre>

5 Comments

Just to clarify, do the <pre> tags go in the body section of the html file?
@MSTTm "Just to clarify, do the <pre> tags go in the body section of the html file?" Yes.
So the body section should look like: <body> <div id="status"></div> <pre></pre> </body>
"So the body section should look like: <body> <div id="status"></div> <pre></pre> </body>" Given html at Question , js at post above, try <body> <pre id="status"></pre> </body> . css defined within style element for #status should still be applied .
Thank you guys so much for your help. However, I'm getting a strange error: "Cannot read property 'appendChild' of undefined"
0

var names = ["John", "Smith", "Paul", "Doe"];
document.getElementById('out').innerHTML = names.join('<br>');
<div id="out"></id>

Comments

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.