jQuery.hasData() method
Example
Set data on an element and return the result of hasData
<
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 »
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 an empty data object), the method returns false; otherwise it returns true.
2. If there is no data object on the element, $.hasData(element) does not create or associate a data object either.
Syntax
$
.hasData( element )
Parameters Description element Element type: A DOM element to check for data
jQuery Miscellaneous Methods