Radley Sustaire
Radley Sustaire

Reputation: 3399

Why are new lines not working in this javascript alert window?

I see quite a few different issues with the alert window and new lines. Most are that the \n is considered a new line in PHP rather than getting sent to javascript.

In my case, the string is being outputted in a new window showing \n. I just tried actually writing \n into an alert box via jsfiddle, and that worked, so it must be my method of doing things...

Here is the string returned to console. as you can see, the \n is definitely there:

Username is required\nPassword is required\nEmail is required\nPhone is required\nCardnumber is required

However, it shows up like this:

An alert with \n instead of a new line

Why is this happening? I think it may have something to do with the data type, as it is returned from $.ajax

if (canAjax && !try_ajax) { 
    e.preventDefault();
    $.ajax({
        type: "POST", 
        url: "mobilesubmit.php",
        data: {"use_ajax": true, "formdata": $("#register_form").first().serializeArray()},
        success: function(data) {

            // This stupid thing should make new lines!
            alert(data);

            console.log(data);
        },
        error: function (request, status, error) {
            try_ajax = true;
            $("#register_form").submit();
        }
    });
}

Upvotes: 9

Views: 26416

Answers (5)

Bathila Sanvidu
Bathila Sanvidu

Reputation: 121

Do not use -> \n

Use this -> \\n

example

alert('Hello Wrold Second line \n\nThanks!') 

// if only above one ddoesn't work use below one!

alert('Hello Wrold Second line \\n\\nThanks!') 
//<!-- Publisher - Bathila Sanvidu Jayasundara -->

Upvotes: 0

Alan S
Alan S

Reputation: 1

IE11 inspect shows the same problem on an asp.net generated hiddenfield - it displays its value as value="test line 1\ntest line 2"

UT actually contains value="test line 1\\ntest line 2"

Using the element.value.replace(/\\n/g,"\n") on it then displays the correct line spaced string in a javascript alert()

Thanks Billy - saved hours of grief

Upvotes: 0

Amit
Amit

Reputation: 738

The most likely cause of this is that your PHP code is escaping the backslash by adding another backslash in front of it. By the time it gets to your JS code, it's actually

"Username is required\\nPassword is required..."

You can inspect the raw response in the network panel of your debugger. If you try to view it in the console, it'll display the formatted output instead of the raw output.

Double-check your method of JSON serialization in your PHP code and make sure it's doing what you expect with the \n.

Upvotes: 4

Billy Moon
Billy Moon

Reputation: 58531

If your console log is showing \n rather than a new line, then it means the \ is escaped in the original string...

Compare

console.log(1,"\\n\\n");
console.log(2,"\n\n");

Solution

use .replace() to swap your \n with newline characters

console.log(3,"\\n\\n".replace(/\\n/g,"\n"))

Upvotes: 20

Ajeet  Sinha
Ajeet Sinha

Reputation: 2345

try adding a space after \n .It should work

Upvotes: 1

Related Questions