Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alert() with new line not used when message comes from an attribute

I have an element

<div id="msg" data-message="new\nline"></div>

when I try to alert using the following javascript.

alert($('#msg').attr('data-message'));

The alert does not perform the new line. Instead displays it exactly as it is in the div attribute.

Output: new\nline

How can I get the new line to work?

Doing this directly does work:

alert('new\nline');

Trying \\n did not work.

like image 349
Valamas Avatar asked Oct 20 '25 01:10

Valamas


1 Answers

the \ is escaped in your string, so try:

alert($('#msg').attr('data-message').replace(/\\n/g,"\n"));
like image 170
Sudhir Bastakoti Avatar answered Oct 22 '25 04:10

Sudhir Bastakoti