最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Newline in alert javascript - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 8

You 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");

发布评论

评论列表(0)

  1. 暂无评论