jQuery.hasData() JQ Home  <<  Utilities  <<  jQuery.hasData()

Non empty data object detection.

Description

The jQuery.hasData() jQuery Data utility method, returns a boolean dependant upon whether an element has any jQuery data associated with it.

  • The jQuery.hasData() method provides a way to determine if an element currently has any values that were set using the jQuery.data() method.

Shorthand version $.hasData()

Syntax

Signature Description
jQuery.hasData( element )Return a boolean dependant upon whether an element has any jQuery data associated with it.
  • If there is no data object or the data object is empty returns false, otherwise returns true.

Parameters

Parameter Description Type
element The DOM element to be checked for data.Element

Return

A Boolean object.

jQuery.hasData( element ) Example Utilities  <<  Top

Return a boolean dependant upon whether an element has any jQuery data associated with it.

In the example below when we press the button the first time we check for arbitrary data before and after attaching it to the 'div12' element below and output some messages.


$(function(){
  $('#btn15').one('click', function(){
    var $div12 = jQuery('#div12');
    $('#div12').append('Arbitrary data attached to the "div12" element: ' +  
                        $.hasData($div12) + '<br>');
    $.data($div12, 'aKey', 101);
    $('#div12').append('Arbitrary data attached to the "div12" element: ' +  
                        $.hasData($div12) + '<br>');
  });
});

div12. Some initial text.

Press the button below to action the above code:

Related Tutorials

jQuery Intermediate Tutorials - Lesson 10 - jQuery Copy, Data & Type Utilities