0

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?

2 Answers 2

1

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

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

or

$('element').data({ someArray: [ 'elem1', 'elem2' ] });
Sign up to request clarification or add additional context in comments.

4 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.