Click here to Skip to main content
15,667,626 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.
Updated 5 days ago

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:
i am good. 

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

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

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:

HI, <br />
i am good.

Hope that helps ^_^
Share this answer
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 :)
Thanks for this, Andy!

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

const addLinebreaks = (anyString) => {
  return anyString.replaceAll("\n", "<br />\r\n");
Share this answer
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
Richard Deeming 5 days ago    
Your question is not a "solution" to someone else's question.

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