Gast1
Gast1

Reputation: 199

Javascript: How do I add a line feed to this code for a textarea field

document.getElementById("hi2").value = arrayd[i] + document.getElementById("hi2").value + ',';

hi2 is a TEXTAREA

When I fill the textarea with the array I don't know how to add a space after every item in the array.

This is the END Result Im looking for:

Jill
Bob
Tony
Nancy

Upvotes: 1

Views: 84

Answers (2)

Farkie
Farkie

Reputation: 3337

Try this:

     <textarea id="hi2"></textarea>
     <script type="text/javascript">
         var arrayd = ['Jill', 'Bob', 'Tony', 'Nancy'];
         document.getElementById("hi2").value = arrayd.join("\n");
     </script>

Upvotes: 0

Dennis
Dennis

Reputation: 32598

You can use Array#join to insert a newline in between each array element

document.getElementById("hi2").value = arrayd.join("\n");

Upvotes: 1

Related Questions