0

I have a variable called hidden as follows in my JS file:

var hidden=$('#hidden').val(); 

When i alert variable hidden, Output is

Need for voice connection\n\ with text messaging pack\n\ and 3G data 

But i need Output as

Need for voice connection with text messaging pack and 3G data 

How to achieve it? In Javascript

5
  • is your #hidden a text area? Commented Apr 29, 2016 at 11:58
  • Seems like the value literally contains the character sequence \n. Why and why doesn't it contain real line breaks? Commented Apr 29, 2016 at 11:59
  • @FelixKling — It's an HTML attribute value. Commented Apr 29, 2016 at 12:00
  • 1
    @reddy - Yes. its a textarea Commented Apr 29, 2016 at 12:00
  • 1
    @Quentin: Apparently it's the value of a textarea, which has no problem containing line breaks: jsfiddle.net/35nqLm3x . Just trying to get the whole context here and finding out whether using actual line breaks from the beginning is possible :) Commented Apr 29, 2016 at 12:04

3 Answers 3

2

So you have a string containing \n\ to represent new lines?

Replace those characters with actual new lines.

var data = "Need for voice connection\\n\\ with text messaging pack\\n\\ and 3G data"; alert("Original: " + data); data = data.replace(/\\n\\ /g, "\n"); alert("Replaced: " + data);

Sign up to request clarification or add additional context in comments.

2 Comments

@Quentin- Perfect. Its Working. What is the workaround for same in JAVA?
I've managed to avoid writing any Java since about 2003. The work around will be the same though (probably, depending on what exactly you are doing with the strings), just with different syntax.
0

This works for me:

alert('test\\n\\test2\\n\\test3\\n\\test4'.replace(new RegExp(/\\n\\/g), '\n');

I'm replacing all excaped \n elements with non-escaped character.

3 Comments

That only replaces the first \n, and the sequence to replace is \n\ anyway. You should probably test it using the string the OP has instead of one which doesn't have the \n\ sequences in it at all
After your edit, it replaces exactly 2 instances of it (and leaves a \ behind). Why not write something generic?
new RegExp(/\\n\\/g) is the same as /\\n\\/g. Only use RegExp if you want to use the value of a string as a regular expression.
0

There is no problem with having text area and you type in some sentences with line breaks, it must output as expected. Here is a demo ..

$('#viewValue').on('click', function() { var hidden = $('#hidden').val(); alert(hidden); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="hidden">Need for voice connection with text messaging pack and 3G data</textarea> <button id="viewValue">view</button>


If at all you are literally typing the characters \n and expecting the line breaks then you must put in some extra efforts to get your desired output.

Replace \n with a new line character using regex. Demo as below

$('#viewValue').on('click', function() { var hidden = $('#hidden').val().replace(/\\n/g, "\n"); debugger; alert(hidden); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="hidden">Need for voice connection \n with text messaging pack \n and 3G data</textarea> <button id="viewValue">view</button>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.