\n new line character won't work
"This is the first line\nAnd this is the second"
I tried making a web page by writing
document.write("This is the first line\nAnd this is the second");
The desired effect is:
This is the first line
And this is the second
But I get this:
This is the first line And this is the second
I tried writing <br> inside and it worked. But Why writing an escape \n didn't? Anything I am doing wrong? I am using firefox
You are doing nothing wrong. The \n character is valid in text related elements like, alert() and <textarea>.
The document.write() command writes to the body of the site or the display. Substitute <br> in place of the \n here.
Ah thank you, the book did not clarify that.
To add to what JMRKER said, whitespace is handled automatically if it's in the body of your page, but not when used in alert boxes and input elements etc. So all your whitespace characters (spaces, new lines etc.) are treated like a single space until the text needs to wrap to the next line, in which case the whitespace character (whatever it is) is treated like a newline character.
Another example that would show this would be writing several spaces after each other:
alert("Hello foo bar."); // shows several spaces between foo and bar
document.write("Hello foo bar."); // shows only a single space
Note also that <p> can be used to replace <br><br>
and can be use in the body to force extra spaces to be visible,
as in to show 5 blank spaces.
could be used to display 9 spaces between visible text characters
Although, the CSS properties 'margin' and 'padding' should be used to add extra space around and inside an element if possible.
Originally Posted by JMRKER
(It feels like we're slowly drifting farther and farther away from the topic by every post. lol)
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)