Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: Invalid or unexpected token

I have a razor syntax like this:

   foreach(var item in model)  { <td><a href ="#"  onclick="Getinfo(@item.email);" >6/16/2016 2:02:29 AM</a>  </td>  } 

My javascript that recieves the request goes like this:

<script type="text/javascript" src="~/Scripts/jquery-1.9.1.js"></script> <script type="text/javascript">     function Getinfo(elem) {         var email = document.getElementById(elem).innerHTML;     } </script> 

When clicking on the href link, I get the following error in the console of the browser:

"Uncaught SyntaxError: Invalid or unexpected token",

and this part is underlined:

    **</a>  </td>** 

I am a beginner so I get stuck in syntax a lot. If it is that then please help me out.

like image 558
Himaan Singh Avatar asked Jun 16 '16 11:06

Himaan Singh


People also ask

What is uncaught SyntaxError unexpected token?

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.

How do you solve uncaught SyntaxError unexpected token export?

To solve the "Uncaught SyntaxError Unexpected token 'export'" error, set the type property to module in your package. json file. Files ending with a . js extension are loaded as ES6 modules when the nearest package.

How do you solve 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 in JavaScript?

The Javascript SyntaxError occurs when trying to interpret code that is not syntactically valid. It is thrown when the Javascript engine comes across tokens or token order that does not conform to Javascript syntax when parsing code.


2 Answers

You should pass @item.email in quotes then it will be treated as string argument

<td><a href ="#"  onclick="Getinfo('@item.email');" >6/16/2016 2:02:29 AM</a>  </td> 

Otherwise, it is treated as variable thus error is generated.

like image 194
Satpal Avatar answered Nov 04 '22 07:11

Satpal


The accepted answer work when you have a single line string(the email) but if you have a

multiline string, the error will remain.

Please look into this matter:

<!-- start: definition--> @{     dynamic item = new System.Dynamic.ExpandoObject();     item.MultiLineString = @"a multi-line                              string";     item.SingleLineString = "a single-line string"; } <!-- end: definition--> <a href="#" onclick="Getinfo('@item.MultiLineString')">6/16/2016 2:02:29 AM</a> <script>     function Getinfo(text) {         alert(text);     } </script> 

Change the single-quote(') to backtick(`) in Getinfo as bellow and error will be fixed:

<a href="#" onclick="Getinfo(`@item.MultiLineString`)">6/16/2016 2:02:29 AM</a> 
like image 42
Iman Bahrampour Avatar answered Nov 04 '22 06:11

Iman Bahrampour