How to use of jQuery.data()?
- jQuery.data() is used to set/return arbitrary data to/from an element.
- Syntax: jQuery.data(element, key, value)
- “element” is the DOM element to which the data is associated.
- “key” is an arbitrary name of the piece of data.
- “value” is value of the specified key.
- Suppose we want to set the data for a span element:
jQuery.data(span, “item”, { val1: 10, val2: “myitem” });
If we want to retrieve the data related to div element and set it to label’s data:
$(“label:val1”).text(jQuery.data(div, “item”).val1);
$(“label:val2”).text(jQuery.data(div, “item”).val2);
See the following links also:
- What is jQuery explain in four steps?
- What is jQuery Selectors? Give some examples.
- In what way can we give fade effect in jQuery?
- Write the animate function in jquery?
- What are the advantages of jquery?
- What is .siblings() method in jQuery ?
- How to mention the css width in jquery code?
- Explain bind() vs live() vs delegate() methods in jquery?
- How to use of jQuery.data()?
- How to write the each() function in jQuery?