Home > Back-end >  syntax error on jquery datatables for unrecognized expression [
syntax error on jquery datatables for unrecognized expression [

Time:02-24

I am checking something with datatables and here is what i have

var array = [];
var element = $('.Page').attr('data-numbers');
array.push(element);
var api = this.api(),
    columns = '['   array   ']';
    alert(columns);
for (var i = 0; i < columns.length; i  ) {
    $('tfoot th').eq(columns[i]).html(api.column(columns[i], {page:'current'}).data().sum());
}

now my data-numbers are coming: data-numbers = '4,5,6,7,8';

This is the Error i get

jquery-3.5.1.js:1677 Uncaught Error: Syntax error, unrecognized expression: [
    at Function.Sizzle.error (jquery-3.5.1.js:1677:8)
    at Sizzle.tokenize (jquery-3.5.1.js:2377:11)
    at Sizzle.compile (jquery-3.5.1.js:2802:12)
    at Sizzle.select (jquery-3.5.1.js:2898:16)
    at Sizzle (jquery-3.5.1.js:894:9)
    at Function.Sizzle.matches (jquery-3.5.1.js:1602:9)
    at Function.jQuery.filter (jquery-3.5.1.js:3071:21)
    at winnow (jquery-3.5.1.js:3057:16)
    at jQuery.fn.init.filter (jquery-3.5.1.js:3101:26)
    at jquery.dataTables.min.js:141:258

CodePudding user response:

Since the columns variable is defined as string, and columns[0] equals '[' character, you cannot use it as an index so it gives the error.

By the way, you can not convert data to an array by only adding "[" and "]" to it.

Can you try this

var array = [];
var element = $('.Page').attr('data-numbers');
array.push(element);
var api = this.api(),
    columns = array[0].split(',')
    alert(columns);
for (var i = 0; i < columns.length; i  ) {
    $('tfoot th').eq(columns[i]).html(api.column(columns[i], {page:'current'}).data().sum());
}
  • Related