Search code examples
javascriptjqueryhtmlcdntablesorter

Trying to use jquery.tablesorter plugin on my table, but it doesn't work


I am trying to use jquery.tablesorter on a simple table to do some practice, I already add CDN link of tablesorter js and css to my HTML <head>. Also, I use every library base on CDN include jQuery, bootstrap, etc. to make the question clearly. I have attached my code below, could anyone help find anything wrong in my code to prevent tablesorter running.

<!DOCTYPE html>
<html>

<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- JQuery -->
    <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
    <!-- Latest compiled and minified CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
        crossorigin="anonymous">
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.29.0/js/jquery.tablesorter.combined.js" integrity="sha256-AQTn9CwiNvZG2zsoT0gswugjZp0alCQySLyt9brT9Cg="
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.29.0/js/jquery.tablesorter.js" integrity="sha256-serXvhbeEKdQIfTFSD3wpNCGNx2+/9py7VXfwLhYTfk="
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.29.0/js/jquery.tablesorter.widgets.js" integrity="sha256-U+0DXO4scYcNVpt7pnud6Fx3KZqK2I5mI6KOeAjSqOE="
        crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.29.0/css/theme.bootstrap_3.min.css"
        integrity="sha256-dXZ9g5NdsPlD0182JqLz9UFael+Ug5AYo63RfujWPu8=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.29.0/css/theme.default.min.css"
        integrity="sha256-kZJ4kB78IbXuxMtCpmaXzii8vxEKtu8pjicH62E0/qM=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.29.0/css/theme.bootstrap.min.css"
        integrity="sha256-dXZ9g5NdsPlD0182JqLz9UFael+Ug5AYo63RfujWPu8=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.29.0/css/jquery.tablesorter.pager.min.css"
        integrity="sha256-x+whz5gQKEXx3S3pxwmxPhC1OWpRiHaPXUW5Yt8/fzg=" crossorigin="anonymous" />
    <style>
    </style>
    <script>

        $(document).ready(function () {
            ("#myTable").tablesorter();
        });
    </script>

</head>


<body>
    <div class="container">
        <div class="col-md-12">
            <table class="table table-striped tablesorter" id="myTable">
                <thead>
                    <tr>
                        <th>Last Name</th>
                        <th>First Name</th>
                        <th>Email</th>
                        <th>Due</th>
                        <th>Web Site</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Smith</td>
                        <td>John</td>
                        <td>[email protected]</td>
                        <td>$50.00</td>
                        <td>http://www.jsmith.com</td>
                    </tr>
                    <tr>
                        <td>Bach</td>
                        <td>Frank</td>
                        <td>[email protected]</td>
                        <td>$50.00</td>
                        <td>http://www.frank.com</td>
                    </tr>
                    <tr>
                        <td>Doe</td>
                        <td>Jason</td>
                        <td>[email protected]</td>
                        <td>$100.00</td>
                        <td>http://www.jdoe.com</td>
                    </tr>
                    <tr>
                        <td>Conway</td>
                        <td>Tim</td>
                        <td>[email protected]</td>
                        <td>$50.00</td>
                        <td>http://www.timconway.com</td>
                    </tr>
                </tbody>
            </table>

        </div>

    </div>


</body>

</html>

Solution

  • As other guys are saying in the comments, you need to put $ to the head of ("#myTable").tablesorter() in order to call it as a Query selector.

     $(document).ready(function () {
         $("#myTable").tablesorter();
     });