By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,149 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,149 IT Pros & Developers. It's quick & easy.

How to handle Multiple line strings in JavaScript?

P: 2

I have stored some vaue that I am getting from textarea to my mysql db like this

Some Data,Some More Data
some Extra data
some More Extra data

and I am getting that data in JSP page
and I try to access that data in my JSP page using JavaScript

like this
var someValue=
Some Data,Some More Data
some Extra data
some More Extra data

but there is multiple line split problem of string
I try to replace by something like this

and that is not working

any help on this topic
Jul 18 '15 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 1,043
The might be just linefeeds ("\r") in the text.....

Did you try this too:
Expand|Select|Wrap|Line Numbers
  1. .replace(/\n|\r/,"#")
if this does not help do this to determince which characters are in the text:
Expand|Select|Wrap|Line Numbers
  1. v="Hallo\r\n"
  2. s=""
  3. for (i=0; i<v.length; i++) { s = s + v.charCodeAt(i); }
The output of this example should be:
Jul 18 '15 #2

P: 2

Thanks for replying

Before I can Post first I would like to know If you have to store value of textarea fields in database and preserve all the line breaks that user has typed

and display back that data back in JSP page with JavaScript what will be your approach for this?
Jul 19 '15 #3

Expert 100+
P: 1,043
"JSP page with JavaScript "

JSP = JavaServer Pages

- Java is not equal to JavaScript
- I do not have (enough) knowledge about JSP
- I would display it so the it will look the same ;)
Jul 19 '15 #4

P: 29
Try This Code
Expand|Select|Wrap|Line Numbers
  1. <!doctype html>
  2.     <head>
  3.         <title>Multi-line JavaScript Strings</title>
  4.     </head>
  5.     <body>
  6.         <div id="var1"></div>
  7.         <div id="var2"></div>
  8.         <div id="var3"></div>
  10.         <script>
  11.             /* JS comes here */
  12.             var myStr1 = `This is my
  13.                 multiline String
  14.                 which is amazing`;
  15.             // adding string value to HTML tag 
  16.             document.getElementById('var1').innerHTML = myStr1;
  18.             var myStr2 = "This is my                 multiline String                 using backslash to escape new line";
  19.             // adding string value to HTML tag 
  20.             document.getElementById('var2').innerHTML = myStr2;
  22.             var myStr3 = "This is my" +
  23.                 " multiline String " +
  24.                 " using concatenation technique";
  25.             // adding string value to HTML tag 
  26.             document.getElementById('var3').innerHTML = myStr3;
  28.         </script>
  29.     </body>
  30. </html>

Expand|Select|Wrap|Line Numbers
  1. This is my multiline String which is amazing
  2. This is my multiline String using backslash to escape new line
  3. This is my multiline String using concatenation technique
3 Weeks Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.