Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a text area.
even when i type text with multiple lines it was taking entire text as a single line in var variable.

example: HI,
i am good.

when i get this into variable i want this as
" HI, '/br tag' i am good. "
but it is coming like " HI,i am good. "

Please help me in this

Thanks in advance.
Posted
Updated 29-May-23 21:04pm

this is a testing to see line break.
I need this for the development of my project.
please help me. I need more clue
 
Share this answer
 
Comments
Richard Deeming 30-May-23 4:21am    
Your question is not a "solution" to someone else's question.
Thanks for this, Andy!

I used this version of your code suggestion. replaceAll let's you change all the instances.

JavaScript
const addLinebreaks = (anyString) => {
  return anyString.replaceAll("\n", "<br />\r\n");
};
 
Share this answer
 
v2
TextArea uses carrage-return('\r'), line-feed('\n') style chars for line breaks. You may not see the chars even in debugger as they will be interpreted to new lines in those displays.

They may well be output to your html page with the line break:
HTML
<div>
HI,
i am good. 
</div>

but the browser will ignore it without the <br />, as you correctly stated

Try this:
C#
//C#
var stringAsHtml = myTextArea.Text.Replace("\r\n", "<br />\r\n");

JavaScript
//Javascript
var stringAsHtml = $(selector).val().Replace("\r\n", "<br />\r\n");


If will preserve the line break in the markup to make it easier to read. the output with then be like this:

XML
<div>
HI, <br />
i am good.
</div>



Hope that helps ^_^
 
Share this answer
 
v3
Comments
Member 9911885 2-Apr-15 11:34am    
thanks for your response. it helped me.
i modified it like this.
post.m_description.replace(/\r\n|\n|\r/g, '<br />');
Andy Lanng 2-Apr-15 11:43am    
yup - that'll work. Thanks for rating :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900