-5

I am facing some problems in the following things. Coz m a beginer in jquery. so please help regarding these. First I want to know:

what is the use of jquery function $.data()

How I use this in my jquery plugin It is good if anyone provide some clarification on this function.

Edwin Alex
  • 5,118
  • 4
  • 28
  • 50

5 Answers5

0

Using $el.data() associates a value to the element and is stored in a object stored in memory by jQuery. This object is keyed by the element itself.

The $.data() method allows you access to all data attributes stored by jQuery, and you can retrieve them by providing the key element required.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • still i am not able to understand its purpose.i have found one jquery calendar plugin in that $.data() function is used to store data.like $this.data(plugin_name).settings.what does it mean.?i am not able to understand its purpose.please help me. – user2902521 Oct 21 '13 at 10:14
  • That's using the standard `$el.data()` function *not* `$.data()`. The full documentation is available. There is really nothing I could add that isn't in there. – Rory McCrossan Oct 21 '13 at 10:16
0

From official doc

Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
0

Well in the early days :-) one would add additional data to dom element by adding additional attributes. jQuery's data created a way of adding additional data to dome element without addind additional attributes.

R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
0

$.data() is used for value of DOM attributes. like we have a dom like

<input id="dom1"  type ="text" data-att1="test1" />

Then you can get the value of attribute data-att1 using

$('#dom1').data('att1');
Anand Jha
  • 10,404
  • 6
  • 25
  • 28
0

In html you can put custom data value in tag. For example :

<li data-numrow="12" data-author="myself">Sample tag</li>

$.data("numrow") can access easely to this data attribute on tag. it more or less a shortcut for $.attr("data-numrow")

Yoann
  • 4,937
  • 1
  • 29
  • 47