How to use jquery in ASP.​NET Core

I suspect your jquery is loaded after the rest of the page content.

This means that you cannot reference jquery objects as the library has not been initialised yet.

Move the page script after jquery has loaded.

<script src="https://stackoverflow.com/questions/37923853/~/lib/jquery/dist/jquery.js"></script>
<script>
  $(document).ready(function () {
    alert("Test"); 
  });
</script>

For efficiency I recommend you do this in one of two ways:


OPTION 1

Use a master script file that loads after jquery.

<script src="https://stackoverflow.com/questions/37923853/~/lib/jquery/dist/jquery.js"></script>
<script src="~/js/master.js"></script>

OPTION 2

Use a placeholder template that will always load after jquery but can be initialised on individual pages.

Master _Layout Page

<script src="https://stackoverflow.com/questions/37923853/~/lib/jquery/dist/jquery.js""></script>
@RenderSection("Scripts", required: false)

Content Page

@section Scripts {
  <script>
    $(function () {
      alert("Test"); 
    });
  </script>
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)