How to add a new line in textarea element?
Try this one:
<textarea cols='60' rows='8'>This is my statement one.
This is my statement2</textarea>
Cross-Browser - Newline characters in textareas
The problem isn't the browser but the operating system. Quoting from this post:
So, using \r\n will ensure linebreaks on all major operating systems
without issue.
Here's a nice read on the why: why do operating systems implement line breaks differently?
The problem you might be experiencing is saving the value of the textarea and then returning that value including any newlines. What you could do is "normalize" the value before saving, so that you don't have to change the output. In other words: get the value from the textarea, do a find-and-replace and replace every ossible occurrence of a newline (\r, \n) by a value that works on all OS's \r\n. Then, when you get the value from the database later on, it'll always be correct.
Read new line from textarea
Basically a white space character(which includes new line chr too) is converted by the HTML to single space.
You will need to write onChange method for the textarea to convert the new line char to either <br>
or <p>
. By this you will be able to hold the property of new line.
Please review this answer:
https://stackoverflow.com/a/29575181/3465242
Newline character in HTML TextArea
The link you've provided actually has an answer.
The textarea element value exists in three variants:
- the raw value as entered by the user, unnormalized; it may contain CR, LF, or CR LF pair;
- the internal value, called “API value”, where
line breaks are normalized to LF (only);- the submission value, where line breaks are normalized to CR LF pairs, as per Internet conventions.
Your case is number 2
Html Textarea - manually entered new line (/n) not working
In HTML, /n
is not a new line, for that matter, nor is \n
.
A new line is a new line (except in places where whitespace is collapsed, which is most of HTML, but textareas are one of the exceptions).
<textarea>This is
not working</textarea>
In JavaScript, \n
is a new line, so JavaScript converts the escape sequence to a new line and then sets the value of the textarea using an actual new line.
Javascript Line Break Textarea
You need to replace line breaks with newline characters: \n
newline character not converting to line break in php text area post
You need to use an actual linebreak in you <textarea>
. \n
wont work there.
nl2br
converts linebreaks to html <br>
.
With php-code you can add a linebreak like this:
<textarea name="comment" rows="5" cols="40">One line.<?= "\n" ?>Another line.</textarea>
Or you spimply write:
<textarea name="comment" rows="5" cols="40">One line.
Another line.</textarea>
If you want to reuse the submitted value:
<textarea name="comment" rows="5" cols="40"><?= htmlentities($_POST['comment']) ?></textarea>
Be careful here, you want to sanitize your user-input with htmlentities($_POST['comment'])
or the user will be able to alter the page.
Additional info
<?= $var ?>
is the short form of <?php echo $var; ?>
References
- htmlentities
Related Topics
How to Force a Web Browser Not to Cache Images
Can an Option in a Select Tag Carry Multiple Values
How to Avoid Joining All Text from Nodes When Scraping
Why Can't I Do ≪Img Src="C:/Localfile.Jpg"≫
How to Add a Browser Tab Icon (Favicon) For a Website
Reading HTML Content from a Uiwebview
How to Force a Line Break in a Long Word in a Div
How to Make an HTML Button Not Reload the Page
Make Footer Stick to Bottom of Page Correctly
Custom HTML Tag Attributes Are Not Rendered by Jsf
Css: Control Space Between Bullet and ≪Li≫
How to Display Just a Portion of an Image in Html/Css
Why Can't I Center With Margin: 0 Auto
How to Use Google Chrome 11'S Upload Folder Feature in My Own Code