Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

41 lines (36 sloc) 2.016 kb
<?xml version="1.0"?>
<entry type="method" name="jQuery.hasData" return="Boolean">
<argument name="element" type="Element">
<desc>A DOM element to be checked for data.</desc>
<desc>Determine whether an element has any jQuery data associated with it.</desc>
<p>The <code>jQuery.hasData()</code> method provides a way to determine if an element currently has any values that were set using <code><a href="/"></a></code>. If no data is associated with an element (there is no data object at all or the data object is empty), the method returns <code>false</code>; otherwise it returns <code>true</code>.</p>
<p>The primary advantage of <code>jQuery.hasData(element)</code> is that it does not create and associate a data object with the element if none currently exists. In contrast, <code></code> always returns a data object to the caller, creating one if no data object previously existed.
<p>Note that jQuery's event system uses the jQuery data API to store event handlers. Therefore, binding an event to an element using <code>.on()</code>, <code>.bind()</code>, <code>.live()</code>, <code>.delegate()</code>, or one of the shorthand event methods also associates a data object with that element.
<desc>Set data on an element and see the results of hasData.</desc>
var $p = jQuery("p"), p = $p[0];
$p.append(jQuery.hasData(p)+" "); /* false */
$.data(p, "testing", 123);
$p.append(jQuery.hasData(p)+" "); /* true*/
$.removeData(p, "testing");
$p.append(jQuery.hasData(p)+" "); /* false */
$p.on('click', function() {});
$p.append(jQuery.hasData(p)+" "); /* true */
$p.append(jQuery.hasData(p)+" "); /* false */
<html><![CDATA[<p>Results: </p>]]></html>
<category slug="data"/>
<category slug="version/1.5"/>
Jump to Line
Something went wrong with that request. Please try again.