1 line
1.9 KiB
JSON
1 line
1.9 KiB
JSON
{"name":"jQuery.hasData","type":"method","title":"jQuery.hasData()","deprecated":null,"removed":null,"desc":"Determine whether an element has any jQuery data associated with it.","categories":["data","version/1.5"],"entries":[{"return":"Boolean","signatures":{"added":"1.5","argument":{"desc":"A DOM element to be checked for data.","name":"element","type":"Element"}},"examples":{"desc":"Set data on an element and see the results of hasData.","code":"\nvar $p = jQuery( \"p\" ), p = $p[ 0 ];\n$p.append( jQuery.hasData( p ) + \" \" ); // false\n\n$.data( p, \"testing\", 123 );\n$p.append( jQuery.hasData( p ) + \" \" ); // true\n\n$.removeData( p, \"testing\" );\n$p.append( jQuery.hasData( p ) + \" \" ); // false\n\n$p.on( \"click\", function() {} );\n$p.append( jQuery.hasData( p ) + \" \" ); // true\n\n$p.off( \"click\" );\n$p.append( jQuery.hasData( p ) + \" \" ); // false\n","html":"\n<p>Results: </p>\n"},"longdesc":"\n <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=\"/jQuery.data/\">jQuery.data()</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>\n <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>jQuery.data(element)</code> always returns a data object to the caller, creating one if no data object previously existed.\n</p>\n <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.\n</p>\n "}]} |