SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Location
    Dartmouth, MA, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code JavaScript:
    $("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().

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhawk90 View Post
    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:

    Code JavaScript:
    $("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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •