jQuery.hasData()Method
Examples
Set data on the element, return the hasData result
<p>Result: </p>
<script>
$(function () {
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.off( "click" );
$p.append( jQuery.hasData( p ) + " " ); // false
})
</script>
Try it Yourself »
Definition and Usage
$The .hasData() function is used to determine whether an element has associated jQuery data.
Note:1. This data is set using $.data(). If an element has no data (no data object at all or the data object is empty), this method returns false, otherwise it returns true.
2. If there is no data object on the element, $.hasData(element) will not create or associate a data object either.
Syntax
$.hasData( element )
| Parameters | Description |
|---|---|
| element | Element type A DOM element used to perform data checks |
Other Extensions
jQuery Miscellaneous Methods