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.
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.
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.
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.
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.
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.
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>
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