Jump to content
Sign in to follow this  
RyanSchurton

how do i start a newline and add space in javascript?

Recommended Posts

var donation = new Array(4);  for (var i = 0; i < donation.length; ++ i) {	 donation [i] = new Array(4)} 	donation [0][0] = "#111";	donation [0][1] = "Jane";	donation [0][2] = "Doe";	donation [0][3] = "$100.00";	donation [1][0] = "#222";donation [1][1] = "John";donation [1][2] = "Doe";donation [1][3] = "$200.00";	donation [2][0] = "#333";donation [2][1] = "Joe";donation [2][2] = "Smith";donation [2][3] = "$300.00";document.write(donation [0][0]);document.write(donation [0][1]);document.write(donation [0][2]);document.write(donation [0][3]);document.write(donation [1][0]);document.write(donation [1][1]);document.write(donation [1][2]);document.write(donation [1][3]);document.write(donation [2][0]);document.write(donation [2][1]);document.write(donation [2][2]);document.write(donation [2][3]);

my output for my code is: #111JaneDoe$100.00#222JohnDoe$200.00#333JoeSmith$300.00 but i need to to look like this: #111 Jane Doe $100.00#222 John Doe $200.00#333 Joe Smith $300.00 with more space between the #111, Jane, Doe and 100.00.

Edited by RyanSchurton

Share this post


Link to post
Share on other sites

I hesittate to support the use of document.write -- but I'll assume your reasons make sense. 1. Try putting your text inside <pre></pre> tags. that should preserve the formatting. 2. Use \t and \n (in quotes) for tabs and newlines.

Edited by Deirdre's Dad

Share this post


Link to post
Share on other sites

Well, I can. I sort of hate to, but I tested this and it "works." I suggest doing a View Source before and after you click the text to execute the function.

<!DOCTYPE html><html>    <head>        <script type="text/javascript">            function writeMe() {                var s = "<pre>hi";                s += "\n";                s += "John";                s += "\t\t";                s += "Smith </pre>";                document.write (s);                document.close();            }        </script>    </head>    <body>        <p onclick="writeMe()">Click me</p>    </body></html>

Edited by Deirdre's Dad

Share this post


Link to post
Share on other sites

Above one is also well & good.But this also is going to work & i've checked it:-donation [0 ][ 0] = "#111 " ;donation [ 0 ][1 ] = " Jane " ;donation [ 0 ][2 ] = " Doe " ;donation [ 0 ][3 ] = " $100.00" ;donation [ 1 ][0 ] = "<br>"+"#222 ";donation [1 ][ 1] = " John " ;donation [1 ][ 2] = " Doe " ;donation [1 ][ 3] = " $200.00" ;donation [ 2 ][0 ] = "<br>"+"#333 ";donation [2 ][ 1] = " Joe " ;donation [2 ][ 2] = " Smith " ;donation [2 ][ 3] = " $300.00";left all same.I've added some "spaces" & <br>.For adding a newline...just go for break.donation[2][0]="<br>"+"#333 ";& add space under " " (double quotes) whereever you want it.

Share this post


Link to post
Share on other sites

An alternative is to put the data in a table. The would give you neat rows and columns to work with. The cell borders are optional and can be styled.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...