Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store an array via jquery data method and add to that array values?

Tags:

jquery

how to store an array via jquery data method and add to that array?

like image 367
som Avatar asked Nov 26 '10 10:11

som


2 Answers

Since references to arrays are stored, you can do it like this:

var array = [1, 2, 3];
$.data(elem, "myArray", array);

// and later
$.data(elem, "myArray").push(4, 5, 6); 

// and later
console.log($.data(elem, "myArray"));
//-> [ 1, 2, 3, 4, 5, 6 ]
like image 78
Andy E Avatar answered Nov 13 '22 09:11

Andy E


Should be simple task:

$('#someelement').data('myarray', []);

// somewhere else
$('#someelement').data('myarray').push('foo');

// access
console.log( $('#someelement').data('myarray')[0] );
like image 31
jAndy Avatar answered Nov 13 '22 09:11

jAndy