The jQuery.hasData Method
The jQuery.hasData method checks whether an element has data set using the data method. If there is no data object associated with the element, the method returns false, otherwise true.
Syntax
This is how we check if a DOM element has data:
jQuery.hasData(DOM element);
Example
Let's use data to set data for the p tag, and check if data is set for it:
<p>text</p>
let $p = jQuery('p'), p = $p[0];
console.log(jQuery.hasData(p)); // will output false
$('p').data('test', 21);
console.log(jQuery.hasData(p)); // will output true
See also
-
method
data,
which allows you to write and read data in an element -
method
removeData,
which deletes the data in the element -
JavaScript method
hasAttribute,
which checks if an element has a given attribute