0

I have a function which receives a single line string and displays it without a problem using an alert pop up from alertify JS but when its a multi-line string it gives an error saying Uncaught SyntaxError: Invalid or unexpected token . Below is my function:

<script>
function changes(changes) {

    alertify.confirm(changes,
        function () {
        }).setHeader('Document Changes');
}

The changes is the string am receiving and am displaying it in an alert box. Below is where am getting the changes from. I am getting this string from an object and passing it to my function

                {
                "render": function (data, type, full, meta) {
                    return '<button onclick="changes(\'' + full.changes + '\')" class="btn btn-info"><i class="fas fa-info-circle"></i> Changes</button>';
                }
            }

When i click this button it does not send the multi-line string to my function but when its a single line string it works without a problem

The sample text which brings the error is like below:

added footer

added heder

added content

Below is a fiddle which i have replicated the error Js Fiddle

2
  • can you provide a sample of changes string that trigger the error? Commented Apr 13, 2022 at 6:57
  • added footer / added heder / added content/ The slashes is the beginning of the new line Commented Apr 13, 2022 at 7:00

2 Answers 2

1

if the problem was multi lines strings, you can use the back ticks ` to print the multi line strings in javascript

alert(`this is \n multi line \n string`)

and in your case would be:

alertify.confirm(`${changes}`,....
Sign up to request clarification or add additional context in comments.

3 Comments

I traced the root of the problem. It is coming from where am receiving the changes string and from there it cant process the string and pass it on. Let me edit my question
alertify.confirm(`${changes}` - useless template use just alertify.confirm(changes
@zb' the usage of alertify.confirm(changes was the heart of the problem.
1

Use the backticks, Luke.

let changes =`
This is a 
multiline
alert
message
`;

alert(changes);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.