<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' href='docs-1.7.xsl'?>
<entries><entry type='method' name="data" return="jQuery">
  <signature>
    <added>1.2.3</added>
    <argument name="key" type="String">
      <desc>A string naming the piece of data to set.</desc>
    </argument>
    <argument name="value" type="Object">
      <desc>The new data value; it can be any Javascript type including Array or Object.</desc>
    </argument>
  </signature>
  <signature>
    <added>1.4.3</added>
    <argument name="obj" type="Object">
      <desc>An object of key-value pairs of data to update.</desc>
    </argument>
  </signature>
  <desc>Store arbitrary data associated with the matched elements.</desc>
  <longdesc><p>The <code>.data()</code> 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.</p>
<p> We can set several distinct values for a single element and retrieve them later:</p>
<pre>
$('body').data('foo', 52);
$('body').data('bar', { myType: 'test', count: 40 });

$('body').data('foo'); // 52
$('body').data(); // {foo: 52, bar: { myType: 'test', count: 40 }}
</pre>
<p>In jQuery 1.4.3 setting an element's data object with <code>.data(obj)</code> extends the data previously stored with that element. jQuery itself uses the <code>.data()</code> method to save information under the names 'events' and 'handle', and also reserves any data name starting with an underscore ('_') for internal use.</p>
<p>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.</p>
<p>Due to the way browsers interact with plugins and external code, the <code>.data()</code> method cannot be used on <code>&lt;object&gt;</code> (unless it's a Flash plugin), <code>&lt;applet&gt;</code> or <code>&lt;embed&gt;</code> elements.</p>
</longdesc>
<example>
    <desc>Store then retrieve a value from the div element.</desc>
    <code><![CDATA[
$("div").data("test", { first: 16, last: "pizza!" });
$("span:first").text($("div").data("test").first);
$("span:last").text($("div").data("test").last);
]]></code>
<css><![CDATA[
  div { color:blue; }
  span { color:red; }
  ]]></css>
                    <html><![CDATA[<div>
    The values stored were
    <span></span>
    and
    <span></span>
  </div>]]></html>
  </example>
<category name="Data"/>
<category name="Data Storage"/>
<category name="Version 1.2.3"/>
<category name="Version 1.4"/>
<category name="Version 1.4.3"/>
<note type="additional">Note that this method currently does not provide cross-platform support for setting data on XML documents, as Internet Explorer does not allow data to be attached via expando properties.</note></entry><entry type='method' name="data" return="Object">
  <signature>
    <added>1.2.3</added>
    <argument name="key" type="String">
        <desc>Name of the data stored.</desc>
    </argument>
  </signature>
  <signature>
    <added>1.4</added>
  </signature>
  <desc>Returns value at named data store for the first element in the jQuery collection, as set by data(name, value).</desc>
  <longdesc>
<p>The <code>.data()</code> 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:</p>
<pre>
alert($('body').data('foo'));
alert($('body').data());
</pre>
<p>The above lines alert the data values that were set on the <code>body</code> element. If no data at all was set on that element, <code>undefined</code> is returned.</p>
<pre>
alert( $("body").data("foo")); //undefined
$("body").data("bar", "foobar");
alert( $("body").data("bar")); //foobar
</pre>
<p><strong>HTML 5 data- Attributes</strong></p>
<p>As of jQuery 1.4.3 <a href="http://ejohn.org/blog/html-5-data-attributes/">HTML 5 data- attributes</a> 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 <a href="http://www.w3.org/TR/html5/elements.html#embedding-custom-non-visible-data-with-the-data-attributes">W3C HTML5 specification</a>.</p>

<p>For example, given the following HTML:</p>

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

<p>All of the following jQuery code will work.</p>

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

<p>Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string. To retrieve the value's attribute as a string without any attempt to convert it, use the <code><a href="/attr/">attr()</a></code> method. When the data attribute is an object (starts with '{') or array (starts with '[') then <code>jQuery.parseJSON</code> is used to parse the string; it must follow <a href="http://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example">valid JSON syntax</a> <em>including quoted property names</em>. 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).</p>
<p>Calling <code>.data()</code> 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 <code>.data(obj)</code>. Using the object directly to get or set values is faster than making individual calls to <code>.data()</code> to get or set each value:</p>
<pre>
var mydata = $("#mydiv").data();
if ( mydata.count &lt; 9 ) {
    mydata.count = 43;
    mydata.status = "embiggened";
}
</pre>
</longdesc>
<example>
      <desc>Get the data named "blah" stored at for an element.</desc>
      <code><![CDATA[
$("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);
});

]]></code>
  <css><![CDATA[
  div { margin:5px; background:yellow; }
  button { margin:5px; font-size:14px; }
  p { margin:5px; color:blue; }
  span { color:red; }
  ]]></css>
  <html><![CDATA[<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>]]></html>
      </example>
  <category name="Data"/>
<category name="Data Storage"/>
<category name="Version 1.2.3"/>
<category name="Version 1.4"/>
<category name="Version 1.4.3"/>
<note type="additional">Note that this method currently does not provide cross-platform support for setting data on XML documents, as Internet Explorer does not allow data to be attached via expando properties.</note></entry></entries>