jQuery | removeData() with Examples

Last Updated : 1 Oct, 2024

The removeData() is an inbuilt method in jQuery that is used to remove those data which are previously set with the data() method.

Syntax:

$(selector).removeData(args);

Here "selector" is the selected element whose previously set data gets removed.

Parameter:

It accepts an optional parameter "args" which specifies the name of the data to remove for the selected element.

Return Value:

It returns the selected element with removed data.

Example: In the below code, the data get removed by removeData() method set by the data() method.

HTML
<html>

<head>
    <script src="https://code.jquery.com/jquery-1.10.2.js">
    </script>
    <script>
        // working of remove data method
        $(document).ready(function () {
            // click here to add data to div element
            $("#b1").click(function () {
                $("div").data("greeting", "Hello Everyone !");
                alert("GeeksforGeeks says : " + $("div").
                    data("greeting"));
            });
            // click here to remove data from div element
            $("#b2").click(function () {
                $("div").removeData("greeting");
                alert("Greeting is: " + $("div").
                    data("greeting"));
            });
        });
    </script>
    <style>
        #b1,
        #b2 {
            padding: 10px;
            margin: 20px;
            background-color: green;
        }
    </style>
</head>

<body>
    <button id="b1">Click here to add data to
        div element</button>
    <br>
    <button id="b2">Click here to Remove data
        from div element</button>
    <div></div>
</body>

</html>

Output:

Comment
Article Tags:

Explore