Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-line alert in Javascript

This code

alert("Hello again! This is how we" + "\n" + "add line breaks to an alert box!");

doesn't work. Firefox JavaScript console names error as "unterminated string literal" and points on " symbol before \n. I want to fire an alert with multi-line text. No jQuery please.

like image 868
Pave Avatar asked Feb 18 '12 09:02

Pave


People also ask

How do I add a new line to an alert?

To add line breaks to JavaScript alert, use “\r\n”.

How do you create a line break in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.


2 Answers

Haven't tested but does this work?

alert("Hello again! This is how we \n add line breaks to an alert box!"); 
like image 171
okyanet Avatar answered Sep 21 '22 04:09

okyanet


This just happened to me... exactly. I needed to change it to \\n instead of \n.

alert("Hello again! This is how we"+"\\n"+"add line breaks to an alert box!"); 
like image 36
gloomy.penguin Avatar answered Sep 20 '22 04:09

gloomy.penguin