What Character Represents a New Line in a Text Area

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:

  1. the raw value as entered by the user, unnormalized; it may contain CR, LF, or CR LF pair;
  2. the internal value, called “API value”, where
    line breaks are normalized to LF (only);
  3. 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



Leave a reply



Submit