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.
the \
is escaped in your string, so try:
alert($('#msg').attr('data-message').replace(/\\n/g,"\n"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With