# .data()

Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

## data ( key, value ) Returns: jQuery version added: 1.2.3

- key
  Type: String
  A string naming the piece of data to set.

- value
  Type: Object
  The new data value; it can be any Javascript type including Array or Object.

## data ( obj ) Returns: jQuery version added: 1.4.3

- obj
  Type: Object
  An object of key-value pairs of data to update.

Description: Store arbitrary data associated with the matched elements.

The `.data()` method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks.

We can set several distinct values for a single element and retrieve them later:

    $( "body" ).data( "foo", 52 );
    $( "body" ).data( "bar", { myType: "test", count: 40 } );
    $( "body" ).data( { baz: [ 1, 2, 3 ] } );
    $( "body" ).data( "foo" ); // 52
    $( "body" ).data(); // { foo: 52, bar: { myType: "test", count: 40 }, baz: [ 1, 2, 3 ] }

In jQuery 1.4.3 setting an element's data object with `.data(obj)` extends the data previously stored with that element. jQuery itself uses the `.data()` method to save information under the names 'events' and 'handle', and also reserves any data name starting with an underscore ('_') for internal use.

Prior to jQuery 1.4.3 (starting in jQuery 1.4) the .data() method completely replaced all data, instead of just extending the data object. If you are using third-party plugins it may not be advisable to completely replace the element's data object, since plugins may have also set data.

Due to the way browsers interact with plugins and external code, the `.data()` method cannot be used on `<object>` (unless it's a Flash plugin), `<applet>` or `<embed>` elements.

## Examples:

#### Example: Store then retrieve a value from the div element.

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>css demo</title>

      <style>

      div {
        color: blue;
      }
      span {
        color: red;
      }

      </style>

      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>

    <div>
      The values stored were
      <span></span>
      and
      <span></span>
    </div>

    <script>

    $( "div" ).data( "test", { first: 16, last: "pizza!" } );
    $( "span:first" ).text( $( "div" ).data( "test" ).first );
    $( "span:last" ).text( $( "div" ).data( "test" ).last );

    </script>

    </body>
    </html>

#### Demo:

## data ( key ) Returns: Object version added: 1.2.3

- key
  Type: String
  Name of the data stored.

## data ( ) Returns: Object version added: 1.4

Description: Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute.

The `.data()` method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks. We can retrieve several distinct values for a single element one at a time, or as a set:

    alert( $( "body" ).data( "foo" ) );
    alert( $( "body" ).data() );

The above lines alert the data values that were set on the `body` element. If no data at all was set on that element, `undefined` is returned.

    alert( $( "body" ).data( "foo" ) ); // undefined
    $( "body" ).data( "bar", "foobar" );
    alert( $( "body" ).data( "bar" ) ); // foobar

#### HTML5 data-* Attributes

As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery's data object. The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the W3C HTML5 specification.

For example, given the following HTML:

    <div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div>

All of the following jQuery code will work.

    $( "div" ).data( "role" ) === "page";
    $( "div" ).data( "lastValue" ) === 43;
    $( "div" ).data( "hidden" ) === true;
    $( "div" ).data( "options" ).name === "John";

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn't change the value's representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.

When the data attribute is an object (starts with '{') or array (starts with '[') then `jQuery.parseJSON` is used to parse the string; it must follow valid JSON syntax including quoted property names. If the value isn't parseable as a JavaScript value, it is left as a string.

To retrieve the value's attribute as a string without any attempt to convert it, use the `attr()` method.

The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).

Calling `.data()` with no parameters retrieves all of the values as a JavaScript object. This object can be safely cached in a variable as long as a new object is not set with `.data(obj)`. Using the object directly to get or set values is faster than making individual calls to `.data()` to get or set each value:

    var mydata = $( "#mydiv" ).data();
    if ( mydata.count < 9 ) {
      mydata.count = 43;
      mydata.status = "embiggened";
    }

## Examples:

#### Example: Get the data named "blah" stored at for an element.

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>css demo</title>

      <style>

      div {
        margin: 5px;
        background: yellow;
      }
      button {
        margin: 5px;
        font-size: 14px;
      }
      p {
        margin: 5px;
        color: blue;
      }
      span {
        color: red;
      }

      </style>

      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>

    <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>

    <script>

    $( "button" ).click(function() {
      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>

    </body>
    </html>

#### Demo: