Getting error "$(..).Datatable is not a function"


When I am trying to add jQuery datatable in my ASP.NET Core project I am getting below error "$(..).DataTable is not a function", here is the error image in Google Chrome console.

datatable-is-not-a-function-min

Although I have added jQuery datatable reference in my code.

<link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<div style="width: 500px">
    <table id="tblEmp" cellpadding="0" cellspacing="0" border="1" style="border-collapse:collapse">
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#tblEmp").DataTable({
                "processing": true, 
                "serverSide": true, // enabling server side
                "filter": true, //set true for enabling searching
                "ajax": { 
                    "url": "/home/person",// ajax url to load content
                    "type": "POST", // type of method to call
                    "datatype": "json" // return datatype
                },
                "columns": [
                    { "data": "firstName", "name": "First Name", "autoWidth": true }, // columns name and related settings
                    { "data": "lastName", "name": "Last Name", "autoWidth": true },
                ]
            });
        });
    </script>

Thanks.


Asked by:- bhanu
1
: 3556 At:- 3/31/2022 12:43:44 PM
Javascript jQuery datatable is not function







1 Answers
profileImage Answered by:- vikas_jk

This error can be due to any one of the following causes:

  • jQuery Multiple versions included
  • jQuery library loaded multiple times.
  • jQuery datatable library is added before jQuery library
    <script type="text/javascript" src="~/Scripts/jquery.js"></script>
    <script type="text/javascript" src="~/Scripts/data-table/jquery.dataTables.js"></script>
    
     <script>$(document).ready(function () {
        $.noConflict();
        var table = $('#MyTable').DataTable();
    });</script>?
  • jQuery DataTables library script reference is missing.

In your .NET project, I will suggest you to wrap all Javascript code as inside "Scripts" section, if you are loading JS/CSS bundles, so if you are loading JS files on the master page (_Layout.cshtml) in .NET MVC project, it works fine.

@section Scripts{
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
 // JS code here
}

also, load jQuery datatable, after jQuery library, something like this

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

to get rid of error.

1
At:- 3/31/2022 12:53:45 PM Updated at:- 12/15/2022 1:52:44 AM
ok, I see, I was loading jQuery library multiple times in _Layout.cshtml. 0
By : bhanu - at :- 3/31/2022 12:56:16 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use