I want to add a newline in alert message in javascript.
var al = $("#txt_noIncorrect").val().toString();
console.log(al); // al='Incorrect amount!\nFor decimals, use point instead of virgule.';
alert(al);
alert('Incorrect amount!\nFor decimals, use point instead of virgule.')
First alert:
Second alert:
Why first alert does not put the newline?
I want to add a newline in alert message in javascript.
var al = $("#txt_noIncorrect").val().toString();
console.log(al); // al='Incorrect amount!\nFor decimals, use point instead of virgule.';
alert(al);
alert('Incorrect amount!\nFor decimals, use point instead of virgule.')
First alert:
Second alert:
Why first alert does not put the newline?
Share Improve this question asked May 21, 2014 at 8:17 POIRPOIR 3,1909 gold badges34 silver badges48 bronze badges 4- 2 sidenote: "point" and "virgule" are in French. The English terms for these punctuation marks are "dot" (or "full stop") and "ma". – The Paramagnetic Croissant Commented May 21, 2014 at 8:20
-
1
Could you try
.text()
instead of.val()
. And drop the.toString()
– Gerben Jacobs Commented May 21, 2014 at 8:21 - works fine in FF. Can make fiddle with example? – deadulya Commented May 21, 2014 at 8:25
-
1
If your
console.log()
shows\n
rather than a new line, then it means that the character \ is escaped. – Mivaweb Commented May 21, 2014 at 8:26
3 Answers
Reset to default 8You could do this alert("Hello \r\nWorld");
You can do this by replacing the \n with a new line character in Javascript using .replace()
alert($("input[type='text']").val().replace(/\\n/g,"\n"));
FIDDLE DEMO
UPDATE
Here is a fiddle example with textarea instead of input text. JSFIDDLE DEMO
try this alert("Hello \n World");