tags:

views:

26

answers:

2

Would you please tell me, in how many ways jquery's .data() can be used.

$("element").data('field','value');

or

$("element").data({'field':'value', 'f2': 'v2' });

is there any other way, we can accompany more data like an array or something?

+1  A: 

You can store any object your like even functions. For an array:

$('element').data('field', [ 'elem1', 'elem2' ]);

or

$('element').data({ someArray: [ 'elem1', 'elem2' ] });
Darin Dimitrov
can't we address every element by field names, like in my example
mrNepal
I am trying to store datas in array and access them like $('element').data('field').('somefield')
mrNepal
Try this: `$('element').data('field', { someField: 'value1', someOtherField: 'value2' });`. And then to retrieve: `$('element').data('field')['someOtherField']`.
Darin Dimitrov
That's it. Exactly what I wanted
mrNepal
A: 
var _fn=function TestFunction(){};
var _obj={};
var _arr=[];
jQuery('elm').data('function',_fn);
jQuery('elm').data('object',_obj);
jQuery('elm').data('array',_arr);
Praveen Prasad