what does jQuery data() function do
Asked Answered
F

4

12

I have not found what is the use of jquery function data(). Can anyone give me some example of how it can be used?

Fickle answered 25/11, 2009 at 5:58 Comment(1)
Do you mean docs.jquery.com/Core/data or docs.jquery.com/Internals/jQuery.data ?Comeau
A
17

Its really useful for associating various objects, strings, arrays, etc with a DOM element. Here is a fun hypothetical use:

$(document).ready(function(){
   $("a").each(function(index, el){
      if(index % 2 == 0) 
         $(this).data('coolColor', 'Orange'); // Set the data
      else 
         $(this).data('coolColor', 'Purple'); // Set the data
   }).click(function(e){
      alert($(this).data('coolColor')); // Retrieve the data
      e.preventDefault();
   });
});

This would select every a tag, and set Orange if its odd, or Purple if its even. This is not the most optimal way to write this code if this is what you really wanted to do, but it does illustrate how to use the .data() function.

You can also use it to store objects:

$("#header").data('headerSettings',{
   color: "red",
   cost:  "$25.00",
   time:  1000
});

Now you could access that data anywhere else on the page:

$("#header").data('headerSettings').color;
Aceous answered 25/11, 2009 at 6:14 Comment(1)
I thi8nk it assigns value to variables just like var color = "orange"Fickle
P
3

I think this question needs a bit more attention. jQuery's data API is really powerful for various use-cases.

jQuery's data function allows you to store and retrieve any associated data with any jQuery object.

Primarily, it can also be used to read data- attributes set on any node in the html.

Example 1

HTML: <div id="myNode" data-foo="bar"></div>.

jQuery Code: $("#myNode").data("foo") //bar

Example 2

Likewise I can store a value w.r.t any node too.

jQuery Code:

$("#myNode").data("foo","baz") $("#myNode").data("foo") //baz

One important thing to note here is, that when one sets a data on the note using the data API, the html is not updated in the DOM. If you want to update the HTML, you might want to stick to the attr("data-foo","baz") method.

While one can read strings stored in HTML data attributes, you can also assign an object while storing a value using the data API.

There are various use-cases where developers link an object with a node.

e.g.

var obj = {
  name : "test"
}
$("#myNode").data("foo",obj);

$("#myNode").data("foo") === obj //true

Go ahead, explore the API here.

Pyrogallol answered 21/1, 2017 at 8:33 Comment(0)
I
2

It allows you to associate any type of data with a DOM element. See this blog post for some examples.

Isagoge answered 25/11, 2009 at 6:1 Comment(0)
V
0

The jQuery documentation sums it up pretty well:

Returns a unique ID for the element.

Typically this function will only be used internally. You will likely not use the data() method in this way. It is called automatically when necessary when using the other data() functionality.

Basically this function exists to support other jQuery functions. It is best to ignore this function as it is not intended to to part of the public interface of the jQuery API.

Varietal answered 25/11, 2009 at 6:0 Comment(1)
You're referring to jQuery.data, while the OP is referring to jQuery.fn.data ... Quite different.Vanitavanity

© 2022 - 2024 — McMap. All rights reserved.