Carriage return for text field (textarea)

I have a textarea with the attribute "wrap" = "hard" (actually this is a text field on the server side, but with several text modes).

<asp:TextBox TextMode=MultiLine runat=server ID=txt Width=50 Height=50 class=txtclass /> <asp:Button runat=server ID=btnServer OnClick=btn_Click Width=80 Text="Click server" /> <input type="button" value="Click client" onclick="clientclick();" id="btnClient" style="width: 80px;" /> protected void Page_Load(object sender, EventArgs e) { txt.Attributes.Add("wrap", "hard"); } 

I enter text that is wider than the text box. When I click the button on the client side, the text is in the warning without a carriage return (for example, "111111111").

 <script src="jquery-1.5.2.min.js" type="text/javascript"></script> <script type="text/javascript"> function clientclick() { alert($('.txtclass').val()); } 

When I press the server button during debugging, I see that the text has a carriage return (for example, "11111 \ r \ n1111").

 protected void btn_Click(object sender, EventArgs args) { var test = txt.Text; } 

The question is, how can I get carriage return text on the client side?

+6
source share
5 answers

There are several people who posted the same question as you, and the general consensus is that there is no simple, turnkey solution. The fix seems to be to move the line one word at a time and add new lines if it is completed.

In this case, some JS example is provided - search and line breaks in textarea, which is ARABIC text

+4
source
 alert($('.txtclass').val().replace(/\r/g,"\r").replace(/\n/g,"\n")); 

Have you tried something like this?

0
source

Since you are dealing with HTML, you can use '<BR>' to cause a line break inside the text field. For some time I struggled with this and finally found this solution. This worked great for me.

0
source

Here's how to do it.

Readable Version:

 S=escape(The_textarea.innerHTML); S=S.replace(/%0D%0A/g,'<br>'); S=S.replace(/%0A/g,'<br>'); S=S.replace(/%0D/g,'<br>'); S=unescape(S); 

Effective Version:

 S=unescape(escape(The_textarea.innerHTML).replace(/%0D%0A/g,'<br>').replace(/%0A/g,'<br>').replace(/%0D/g,'<br>')); 
0
source

The carriage returns \n\r still exist, they simply are not interpreted (modified in HTML).

Therefore, use the <br /> elements - on the client side they will be interpreted as HTML and will display line breaks as desired.

0
source

Source: https://habr.com/ru/post/895142/


All Articles