Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: Invalid or unexpected token for new line

I am trying to print the string of a PHP variable in a Javascript function.Whenever i use a new line in the string that is stored in the PHP variable, I get this error Uncaught SyntaxError while printing it out in a Javascript function.

enter image description here

like image 634
Jahidul Haque Pathan Avatar asked Aug 13 '17 14:08

Jahidul Haque Pathan


People also ask

What does uncaught SyntaxError invalid or unexpected token mean?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

How do I fix uncaught SyntaxError unexpected identifier?

To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.

What is uncaught SyntaxError unexpected token?

Last reviewed in August 2021. The error Uncaught SyntaxError: Unexpected token < is most commonly caused by your site's code referring to an asset that is no longer available. Most commonly, this is due to a filename change in assets generated during your build.


1 Answers

If you want to declare a string variable across multiple lines, you could use this syntax:

var str = `line 1
  line 2
  line 3`;

Note the (`) instead of (")/(') double or single quotes. They can't be used to declare multiline string.

Essentially, you will need to write it like this:

jQuery("body").replaceWith(`<?php echo $PHPVARIABLE?>`)
like image 184
Nisarg Shah Avatar answered Sep 21 '22 06:09

Nisarg Shah