0

When, i set value to a textarea (programatically) as below, my string containing carriage return character, is removed.

document.getElementById('id').value = '<UNICODE-WIN>\r\n<Version:12.1>';
document.getElementById('id').value; //'<UNICODE-WIN>\n<Version:12.1>'
//or
$("#id").prop('value', '<UNICODE-WIN>\r\n<Version:12.1>');
$("#id").val(); // '<UNICODE-WIN>\n<Version:12.1>
//or
$("#id").val('<UNICODE-WIN>\r\n<Version:12.1>');
$("#id").val(); // '<UNICODE-WIN>\n<Version:12.1>

As i, researched there is no solution to this problem, still looking for a way to handle this scenario. \r\n is important for us. As soon we set value for element, \r gets replaced. Any help will be great. Thanks for your time.

Researched alot on this but have no idea how to resolve this. These are browser level encoding/replacement. Just looking for a way to set this behavior off. Read Those and think there is no way. https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea-api-value Difference between \n and \r? carriage return in textarea retrieved as line feed

Workaround: It seems that below will not work

$("#id").prop('value', '<UNICODE-WIN>\r\n<Version:12.1>');
$("#id").val(); // '<UNICODE-WIN>\n<Version:12.1>

But this will work for other custom prop (seems they specifically kill it.)

$("#id").prop('plaintextprop', '<UNICODE-WIN>\r\n<Version:12.1>');
$("#id").prop('plaintextprop'); // '<UNICODE-WIN>\r\n<Version:12.1>
  • Can you please edit the question to show the behaviour which removes the carriage return. I ask, as this is not the default behaviour, so there must be some code that *you are using* which is doing this - possibly even just the attributes in the HTML may prevent the line breaks. – Rory McCrossan Feb 23 '23 at 08:42
  • If the textarea does not retain it then its not something you can use. Perhaps another solution is using dbl backslash n, to emulate the backslash r, and then on reading it add it back in. Or use another special character type that is invisible to the user - though not sure what that would be. Consider also that the user who edits the textarea, when they use Enter to enter carriage return it could easily be a mix of one of the two and its probably a lot cleaner only having backslash n anyway for specific amount of line breaks emulating a paragraph. – Steve Tomlin Feb 23 '23 at 08:50
  • RoryMcCrossan and steve thanks for commenting, we need these specific string without removing anything. Its my browser's behavior. – Anon Coder Feb 23 '23 at 08:53
  • Per W3C : A control's value is its internal state. As such, it might not match the user's current input. – Anon Coder Feb 23 '23 at 11:36

0 Answers0