I’m working on a web app where I need to create table rows dynamically and make them sortable. I’m using the HubSpot Sortable library but keep getting an error that says the Sortable object is not defined. The JavaScript file is loading properly as far as I can tell, but when I try to call the methods it throws a ReferenceError.
Error message:
ReferenceError: Sortable is not defined
My code setup:
// HTML head includes
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="/static/js/sortable-lib.js"></script>
// Main JavaScript
$(document).ready(function() {
$("#loadData").click(function() {
$.ajax({
url: "/api/data/",
method: "GET",
dataType: "json",
success: function(response) {
$('#content-area').empty();
var activeIcon = '<span class="status-active">●</span>';
var inactiveIcon = '<span class="status-inactive">○</span>';
$('#content-area').append("<table class='data-table' id='sortable-table'><thead><tr><th>Server Name</th><th>Status</th><th>Actions</th></tr></thead><tbody>");
for (var i = 0; i < response.servers.length; i++) {
var server = response.servers[i];
var statusIcon = server.active ? activeIcon : inactiveIcon;
$('#sortable-table tbody').append('<tr><td>' + server.name + '</td><td>' + statusIcon + '</td><td>' + server.commands + '</td></tr>');
}
// This is where the error occurs
Sortable.init();
var tableElement = document.querySelector('#sortable-table');
Sortable.initTable(tableElement);
}
});
});
});
I’ve tried calling Sortable.init() and also tried without it, but both approaches give me the same undefined error. Has anyone experienced this issue with the HubSpot Sortable library before?