JQuery Programmer Question:

Explain what is the use of jQuery.data()?

Tweet Share WhatsApp

Answer:

1. jQuery.data() is used to set/return arbitrary data to/from an element.
2. Syntax: jQuery.data(element, key, value)
3. "element" is the DOM element to which the data is associated.
4. "key" is an arbitrary name of the piece of data.
5. "value" is value of the specified key.
6. 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);

Download JQuery Programmer PDF Read All 201 JQuery Programmer Questions
Previous QuestionNext Question
How to attach a function to be executed whenever an AJAX request completes using jQuery?Explain what does dollar sign ($) means in jQuery?