JQuery hasData() method

This hasData() method in JQuery is used to determine whether an element has any jQuery data associated with it. This data may be text, event associated with element. There are two examples discussed below:

Syntax:

jQuery.hasData(element)

Arguments:

  • element: This parameter is a DOM element which is to be checked for data.
  • Example: There is no data associated with <div> so the method returns false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML> 
    <html>  
    <head
        <title
          JQuery | hasData() method
        </title>      
        </script
    </head>   
    <body style="text-align:center;"
        <h1 style="color:green;">  
            GeeksForGeeks  
        </h1
        <p id="GFG_UP"
        </p>
        <div> This is DIV
        </div>
        <br>
        <button onclick="Geeks()"
            Click here 
        </button>       
        <p id="GFG_DOWN"
        </p>       
        <script
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            var $div = jQuery( "div" ), div = $div[ 0 ];
            el_up.innerHTML = "JQuery | hasData() method";
            function Geeks() { 
                el_down.innerHTML = jQuery.hasData(div);
            
        </script
    </body>   
    </html

    chevron_right

    
    

  • Output:
  • Example: There is a event associated with <div> so the method returns true.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML> 
    <html>  
    <head
        <title
          JQuery | hasData() method
        </title>      
    </script
    </head>   
    <body style="text-align:center;"
        <h1 style="color:green;">  
            GeeksForGeeks  
        </h1
        <p id="GFG_UP"
        </p>
        <div> This is DIV
        </div>
        <br>
        <button onclick="Geeks()"
            Click here 
        </button>       
        <p id="GFG_DOWN"
        </p>       
        <script
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            var $div = jQuery( "div" ), div = $div[ 0 ];
            el_up.innerHTML = "JQuery | hasData() method";
            $div.on( "click", function() {} );
            function Geeks() { 
                el_down.innerHTML = jQuery.hasData(div);
            
        </script
    </body>   
    </html

    chevron_right

    
    

  • Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.