1

I have here a working code regarding arrays but i want my input to display in new lines not a single row. I have tried \n but no luck.

Here is my code:

<html>
    <meta charset="UTF-8">
    <head>
        <title>Sample Array</title>
        <script>var index = 0;</script>
    </head> 

    <body>


        Input:
        <input type="text" id="input">
        <input type="button" value="GO" onClick="press(input.value)">

        <p id = "display">

        <script>

            var sample = new Array();

            function press(Number)
            {

                if ( Number != '' )
                {
                    sample[index] = Number;
                    document.getElementById("display").innerHTML += sample[index] + "\n";
                    index++;

                }   

                else
                    alert("empty");
            }       

        </script>

    </body>
</html> 

I want to code the newline within the document.getElementById. Thanks in advance.

1
  • i forgot about that. thanks. Commented Jan 16, 2014 at 18:08

1 Answer 1

4

You're dealing with HTML which treats most white space characters as a single space. Just concatenate a <br>:

document.getElementById("display").innerHTML += sample[index] + "<br>";
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks alot! works like charm. Also i want to add something, is there any way to clear the input box after i press the button?
I can now continue with my project. Thanks for the help.
For the record, document.getElementById('input').value = '', @MarcIntes. And it's safer to always use getElementById; referencing the id directly works on most (all?) browsers, but it's non-standard.

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.