NoWar
NoWar

Reputation: 37633

$(document).ready doesn't work under MVC4 project

I am just inserting

$(document).ready(function () {
    alert("!!!");
});

as usually into

@{
    ViewBag.Title = "Sign Up";
    Layout = "~/Views/Shared/_WebSite.cshtml";
}

<script type="text/JavaScript">

    $(document).ready(function () {
        alert("!!!");
    });

</script>

<h2>Sign Up</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
{

}

}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

but I cannot see alert() message...

other things of jQuery are working fine at this page...

What do I am missing?

Any clue?

Thank you!

Upvotes: 10

Views: 22267

Answers (1)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38478

You can see jQuery is referenced after your code if you inspect the html.

Default project comes with an optional scripts section that will be rendered after jQuery reference in the layout, that's where your code should go.

@{
    ViewBag.Title = "Sign Up";
    Layout = "~/Views/Shared/_WebSite.cshtml";
}



<h2>Sign Up</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
{

}

}

@section Scripts {
    <script type="text/JavaScript">

    $(document).ready(function () {
        alert("!!!");
    });

    </script>
}

Upvotes: 31

Related Questions