4

How can I preserve a carriage return character in a text area?

textarea.value = "X" + String.fromCharCode("13") + "X";
textarea.value.charCodeAt(1); //returns 10, not 13

See here: http://jsfiddle.net/vah9e/

tckmn
  • 57,719
  • 27
  • 114
  • 156
cvazac
  • 881
  • 6
  • 7
  • Review this one: http://stackoverflow.com/questions/1761051/difference-between-n-and-r , I think you can use 10 instead of 13 – Simcha Mar 01 '15 at 08:27
  • This is unfortunate. It seems that there is no HTML element that will preserve user input for these types of characters. Suppose I was providing the user an input to search their data - I can't control their data. `\r` and `\n` may be meaningful to them. – cvazac Mar 01 '15 at 17:47

1 Answers1

1

According to W3C Textarea api value, it seems that when invoking the .value attribute of textarea, any carriage return or line feed is transformed into a LINE FEED (10) character (in fact W3C says CRFL but it seems the browsers prefer only LF) - so a script can be platform-independent on line feeds.

In a form data, it seems that the fine feeds are transformed into CRLF (13+10).

W3C speaks also about a raw value but it's probably internal to any javascript engine and it seems it's not available in Javascript.

Juergen
  • 12,378
  • 7
  • 39
  • 55
Arglanir
  • 200
  • 1
  • 9