Ibrahim Amer
Ibrahim Amer

Reputation: 1218

Uncaught SyntaxError: Unexpected token ILLEGAL onclick mvc

I know that this is question has been asked many times before but I can't find any similar case like mine.

I want to call a javascript function on an input type button control I'm doing it like so:

<input type="button" onclick="FormatApplicationMessage(@application.StatusID, '@application.IssueMessage')" class="fa fa-search fa-lg" />

but I always got this error:

Uncaught SyntaxError: Unexpected token ILLEGAL

This is the generated html :

<input type="button" onclick="FormatApplicationMessage(5, 'Please provide a copy of your CV')" class="fa fa-search fa-lg" />

I tried to do this:

<input type="button" onclick='FormatApplicationMessage(@application.StatusID, "@application.IssueMessage")' class="fa fa-search fa-lg" />

But I get the same error message

any hacks ?

EDIT

Here is FormatApplicationMessage implementation:

function FormatApplicationMessage(statusID, issueMsg) {
    //declined
    if (statusID == 4) {
        $('#ApplicationMessageLabel').val("Message");
    }
        //Update Required
    else if (statusID == 5) {
        $('#ApplicationMessageLabel').val("Message");
    }
    //Approved
    else if (statusID == 6) {
        $('#ApplicationMessageLabel').val("Message");
    }
    $('#ApplicationMessage').show();
}

Upvotes: 0

Views: 1134

Answers (2)

Ibrahim Amer
Ibrahim Amer

Reputation: 1218

After days of investigation finally I found the problem. The problem was very silly and quite annoying

The rendered HTML was like so:

<i  class="mywarning fa fa-warning (alias) fa-lg" onclick="FormatApplicationMessage(5,&quot;Return Message 
&quot;)"></i>

as you can see this part &quot;)"></i> is rendered on the next line because the string contains endlines which for some reasons it isn't legal to do so.

So to solve the problem I'd just replaced endlines by '< br >' (you can replace it by whatever symbol you like) when calling FormatApplicationMessage for the second parameter. Inside FormatApplicationMessage I restored those '< br >' to "\n" then displayed it in a text area.

USEFUL THREADS:

1

2

3

4

Upvotes: 0

bash.d
bash.d

Reputation: 13207

Here is what I think might go wrong:

  1. FormatApplicationMessage is not defined as a function. Define this function with two parameters. And reference the script or define the function inline.
  2. Make sure that in your view you can access @application and it contains your properties StatusID and IssueMessage
  3. Make sure that @application is not part of your @Model or @ViewBag. If either is true use either @Model.application.XYZ or @ViewBag.application.XYZ

If all is defined, and you want to use @application inside parenthesis, use @(application.XYZ) otherwise the parser might get an error, so try:

 <input type="button" onclick='FormatApplicationMessage(@(application.StatusID), "@(application.IssueMessage)")' class="fa fa-search fa-lg" />

See http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/ and look for Explicit Expression

EDIT

If @Infas is a C# class and you want to use this in JavaScript you must create the script inline in your view. You cannot use Razor @-syntax in a pure .js-file.

Upvotes: 1

Related Questions