Handy Function to check if jQuery Datatable exists

check if jQuery Datatable exists

jQuery Datatable is a powerful jQuery plug-in used to display the data in listing at front-end. These datatables handle our data to show in a user friendly manner and provide lot of features to use like sorting, searching, paging, and export to different types of file and print.

jQuery datatable provides all these features in very user-friendly and easier to use manner. But while using such DataTable, first we need to check if that jQuery DataTable instance exists or not.

Sometimes we have already  created an instance of jQuery DataTable. And later again try to create one more instance of that same existing jQuery Datatable. In such case the Datatable will give us a warning: Cannot reinitialize jquery datatable.

So for preventing this type of warning, first we need to check if jQuery DataTable exists before doing any thing. For this we can use $.fn.dataTable.isDataTable().

how to check if jQuery Datatable exists

$.fn.dataTable.isDataTable() method provides the ability to check if a table node is already a DataTable or not. It can be accessed at any time, even before any DataTable has been created on the page. isDataTable() is a function of $.fn.dataTable object.

Syntax :


Check if #dtTable is a existing DataTable or not. If not, initialize it:

So this quite handy function is here to check if jQuery Datatable exists. Before processing anything on DataTable instance and it will make you to properly initialize it and keep warning away.

You Might Interested In