Andre Lombaard
Andre Lombaard

Reputation: 7105

Undefined javascript function in MVC view

I have a simple javascript function

function SayTest() {
    alert("test");
}

I include the javascript file in my MVC 4 view

<script type="javascript" src="~/Scripts/MyScripts/MyScript.js"></script>

and call the SayTest function from within my view

<script type="text/javascript">
    $(document).ready(function () {
        $("#myBtn").click(function () {
            SayTest();
        });
    });
</script>

I'm getting an undefined function error. This is driving me nuts. If the js file was included and the function is define, why do I get an undefined function error?

Upvotes: 0

Views: 5069

Answers (2)

Microsoft DN
Microsoft DN

Reputation: 10030

Try using :

<script type="text/javascript" src="~/Scripts/MyScripts/MyScript.js"></script>

Upvotes: 3

rossipedia
rossipedia

Reputation: 59437

You probably need to include jQuery into your View page:

<script type="text/javascript" src="~/Scripts/jquery.min.js"></script>

Make sure that's before any code that calls $().

(Note: You might have to change the exact filename to match what's in your project).

Upvotes: 0

Related Questions