Dev Kumar
Dev Kumar

Reputation: 117

'jQuery' is not defined error - Not Identified

_Layout.cshtml contains:

In Header

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")

In Footer

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryval") 
@RenderSection("scripts", required: false)

In View Code added:

<script type="text/javascript">
    $(document).ready(function () {
        alert('hello');
    });
</script>

And no alert message displayed. There is something wrong with the script calling ?

Upvotes: 0

Views: 3197

Answers (4)

jindhk
jindhk

Reputation: 61

check your bundleconfig.cs if it contains the following..

 bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.js"));

make sure your jquery is in the Scripts folder.

try to use jquery in the header at first place.

Upvotes: 1

Balaji Marimuthu
Balaji Marimuthu

Reputation: 2058

Verify your code under the jQuery scripts. Or move the scripts to the header section.

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)

Your code should be below the jQuery scripts.

$(document).ready(function () {
    alert('hello');
});

Upvotes: 1

NTI
NTI

Reputation: 83

Well, try:

In Header

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")

In Footer

@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryval") 
@RenderSection("scripts", required: false)

Upvotes: 2

Shyju
Shyju

Reputation: 218732

This error usually happens when you try to use jQuery before it is included in the page.

Make sure your javascript code in the view is under the scripts section

@section scripts
{
   <script type="text/javascript">
    $(document).ready(function () {
        alert('hello');
    });
   </script>
}

Assuming you have a RenderSection method call for this section in your layout after loading jQuery library

@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)

When razor executes the page, it replaces the scripts RenderSection call with the javascript code defined in the scripts section of the view.

Upvotes: 1

Related Questions