Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • jQuery Misc data() Method

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 97
    Comment on it

    Hello Readers,


    jQuery data method is usually used to set and get data functionality.


    Attach data to a HTML tag(like div or span) element, then retrieve the data.
    The jQuery data() method attaches data to, or gets data from, selected elements.


    Syntax:  $(selector).data(name)
    


    Example:

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("#setBtn1").click(function(){
            alert($(".setData").data("getSetKey"));
        });
        $("#setBtn2").click(function(){
            alert($(".setData").data("getSetKey","This is the value to print"));
        });
    });
    </script>
    </head>
    <body>
    <button id="setBtn1">First attach data to div element</button><br>
    <button id="setBtn2">Second get data attached to div class (setData) element</button>
    <div class=".setData"></div>
    </body>
    </html>
    


    Thanks


 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: