Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

syntax error on jquery datatables for unrecognized expression [

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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());
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading