Reputation: 742
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
Upvotes: 0
Views: 1680
Reputation: 18987
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>
Upvotes: 0
Reputation: 7441
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.
Upvotes: 0
Reputation: 943108
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);
Upvotes: 2