Many times we need to add some data to DOM elements. Like if you want to hide particular div on click of a button, In such cases I used to use class of button to store id of div which we want to hide. Or may be we want to keep a counter of how many times a button is clicked.
Using id or class or JavaScript variable serves the purpose but many times it gets bit complicated. jQuery data() methods provides a cleaner approach to associate arbitrary data to DOM elements.
Method Signature:
For more details : http://api.jquery.com/data/
Using id or class or JavaScript variable serves the purpose but many times it gets bit complicated. jQuery data() methods provides a cleaner approach to associate arbitrary data to DOM elements.
Method Signature:
.data(key,value);
key : A string naming the piece of data to set.
value : The new data value; it can be any Javascript type including Array or Object.
In given jsFiddle, using data method some value is stored for span with id="foo"
For more details : http://api.jquery.com/data/
No comments:
Post a Comment