how to store an array via jquery data method and add to that array?
3 Answers
Should be simple task:
$('#someelement').data('myarray', []);
// somewhere else
$('#someelement').data('myarray').push('foo');
// access
console.log( $('#someelement').data('myarray')[0] );
2 Comments
Nick Craver
Why not a global variable if you're just storing it on body? :) I have my doubts that
.data() or $.data() is even appropriate here...jAndy
@NickCraver: I modified it for your well-being :p
var array=[1,3,5];
var count=7;
$('#id').data('array').push(count);
1 Comment
ohcibi
You should set
array as the data of the element. Otherwise you'd be pushing to undefined.