jQuery Data - hasData() Method
Example
Set data on the element and then check the result of hasData:
$(function(){ var $p = jQuery("p"), p = $p[0]; $p.append(jQuery.hasData(p)+" "); /* false */ jQuery.data(p, "testing", 123); $p.append(jQuery.hasData(p)+" "); /* true */ jQuery.removeData(p, "testing"); $p.append(jQuery.hasData(p)+" "); /* false */ });
Definition and usage
The hasData() method checks if the element has any jQuery data associated with it.
Syntax
jQuery.data(element)
Parameters | Description |
---|---|
element | Optional. The DOM element whose data needs to be checked. |
Detailed description
jQuery.hasData(
jQuery.data(elementThe main advantage of this is that it does not create a data object and associate it with the element if there is no data object.elementAlways returns a data object to the caller, or creates one if it does not exist previously.