1

I have the following span element defined in the view in the MVC app

<span id="openDocument" 
      class="fa fa-sign-in" 
      style="cursor:pointer; 
      color:#5d9cec" 
      data-toggle="tooltip" 
      onclick="openDocument(@item.formattedReference.ToString())")>
</span>

Javascript function:

function openDocument(documentId) {
    debugger;
    window.open("https://somelongURL/documentId=" + documentId);
};

The problem that I am having with this code is that this code works for document id that has numbers (eg. 12345) but it doesn't work for alphanumeric numbers (eg. R12345).

It gives me an error saying: Uncaught Reference Error: R12345 is not defined.

How do i solve this problem?

Thanks in advance.

1 Answer 1

3

Try to put your variable in quotes:

<span ... onclick="openDocument('@item.formattedReference.ToString()')"></span>

Unlike numbers, strings can only be passed in quotation marks.

Explanation:

Uncaught Reference Error: R12345 is not defined.

The error message contains all information about debugging, namely that the transmitted value is interpreted as a variable as soon as it contains letters. R12345 was interpreted as a variable.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot for the insight. that was really helpful :)
Note you do not need the .ToString()

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.