Atma Atma - 1 month ago 7
Javascript Question

javascript syntax error setting property of jquery datatable

I am using the jquery data table like this:

$.fn.tkDataTable = function(){
"order": [[ 7, 'asc' ]]

if (! this.length) return;

if (typeof $.fn.dataTable != 'undefined') {

this.dataTable();

}

};


I get the error:


Uncaught SyntaxError: Unexpected token :


I get this on the line with "order". How can I correctly set this attribute without the syntax error?

Answer

You rarely call datatable without an options object

I suspect what you want is something like

$.fn.tkDataTable = function(){
    var opts = {
       "order": [[ 7, 'asc' ]],
       //...  other initialization properties
    };

    if (! this.length) return;

    if (typeof $.fn.dataTable != 'undefined') {
        // pass options to plugin
        this.dataTable(opts);

    }
}
Comments