jQuery's data action

Hi - could someone kindly clarify if my understanding of what a data action is, is correct.

Data action to me seems like a way to create a static property within a user defined object or element, such that that property acts as a constant across all the instances of that particular type of object within which it’s added.

Am I way off (:

It’s just a way to store extra data on an element. Instead of using custom HTML attributes, the data is stored internally by jQuery. So for example, if you wanted to keep track of which table cells have been clicked for whatever reason, you can do this:


$("td").click(function(e) {
  if ($(this).data("clicked")) {
    alert("You already clicked this cell");
  } else {
    $(this).data("clicked", true);
    alert("Clicked this cell for the first time");
  }
});

It’s not limited to simple values like in the code above though, you can store references to other HTML elements as well as other JavaScript objects using .data().

thanks bhawk90, the example was useful, made a note of it in my snippets app. :slight_smile: