Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get all data which stored in jquery.data()

Tags:

jquery

this is the demo code:

  $(document).ready(function(){

    $("button").click(function(e) {
      var value;

      switch ($("button").index(this)) {
        case 0 :
          value = $("div").data("blah");
          break;
        case 1 :
          $("div").data("blah", "hello");
          value = "Stored!";
          break;
        case 2 :
          $("div").data("blah", 86);
          value = "Stored!";
          break;
        case 3 :
          $("div").removeData("blah");
          value = "Removed!";
          break;
      }

      $("span").text("" + value);
    });

  });
  </script>
  <style>
  div { margin:5px; background:yellow; }
  button { margin:5px; font-size:14px; }
  p { margin:5px; color:blue; }
  span { color:red; }
  </style>
  <div>A div</div>
  <button>Get "blah" from the div</button>
  <button>Set "blah" to "hello"</button>
  <button>Set "blah" to 86</button>
  <button>Remove "blah" from the div</button>
  <p>The "blah" value of this div is <span>?</span></p>

but , how to get all data(because some key name i don't know ) ??

thanks

like image 202
zjm1126 Avatar asked Aug 31 '10 00:08

zjm1126


2 Answers

You can call .data() without any arguments to get all an object with all the entries, like this:

$("div").data() //returns object

This object will have all they keys available, for example if:

$("div").data("thing") //returns "value"

Then $("div").data() would return at least:

{ thing: "value" }
like image 119
Nick Craver Avatar answered Sep 28 '22 16:09

Nick Craver


JQuery documentation: http://api.jquery.com/data/#data

$("div").data();
like image 44
Matt Ball Avatar answered Sep 28 '22 17:09

Matt Ball