jQuery remove data attribute from elements. Through this tutorial, i am going to show you how to remove data attribute value from HTML elements using removeData() method.
Remove data attributes from HTML using jQuery
Use the Query removeData() method to removes data attribute previously set on HTML elements.
Syntax jQuery removeData() method
$(selector).removeData(name)
Parameters of jQuery removeData() method
Parameter | Description |
---|---|
name | It is an Optional parameter. Specifies the name of data attribute to remove. If no name is specified, this method will remove all stored data attribute from the selected HTML elements |
Example – jQuery Remove Data Attribute
See the following example for remove data attributes from HTML using jQuery; as shown below:
<!DOCTYPE html> <html> <head> <style> div { margin:2px; color:blue; } span { color:red; } </style> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> </head> <body> <div>value1 before creation: <span></span></div> <div>value1 after creation: <span></span></div> <div>value1 after removal: <span></span></div> <div>value2 after removal: <span></span></div> <script> $("span:eq(0)").text("" + $("div").data("test1")); $("div").data("test1", "VALUE-1"); $("div").data("test2", "VALUE-2"); $("span:eq(1)").text("" + $("div").data("test1")); $("div").removeData("test1"); $("span:eq(2)").text("" + $("div").data("test1")); $("span:eq(3)").text("" + $("div").data("test2")); </script> </body> </html>
Recommended jQuery Tutorials
Recommended:-jQuery setInterval and setTimeout
Recommended:-jQuery Select Multiple Classes
Recommended:-jQuery Registration Form validation using Validator
Recommended:-jQuery Form Validation Custom Error Message
Recommended:-jQuery Get Set and Delete Div Background Image
Recommended:-jQuery Get Current Page URL, Path, Host and hash
Recommended:-jQuery Remove Duplicate Objects from Array
Recommended:-jQuery Convert Image into base64 String
Recommended:-jQuery Range UI Slider With Tooltip, Labels
Recommended:-jQuery Reload or Refresh Page Automatically
Recommended:-jQuery Remove Elements From array and Object
Recommended:-jQuery Remove All Space From String
Recommended:-jQuery Ajax Form Submit FormData Example
Recommended:-jQuery Get and Set Image Src Example
Recommended:-JavaScript / jQuery Calling Rest Api Example
Recommended:-jQuery Remove Whitespace From String
Recommended:-jQuery Multiple Image Upload With Preview and Delete
Recommended:-jQuery Ajax Loading Spinner