jQuery | data() with Examples Last Updated : 27 Oct, 2021 Comments Improve Suggest changes Like Article Like Report The data() is an inbuilt method in jQuery which is used to attach data or get data for the selected elements. Syntax: $(selector).data(para1); Parameter : It accepts an optional parameter "para1" which specifies the name of the data to retrieve for the selected element. Return Value: It returns the retrieved data for the selected element.jQuery code to show the working of data() method: Code #1: In the below code, data is attach to the selected element. html <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/ jquery/3.3.1/jquery.min.js"></script> <style> div { display: block; width: 500px; font-size: 37px; padding: 50px; background-color: lightgrey; } span { color: green; } </style> </head> <body> <div> Welcome to <span></span>for<span></span>! </div> <script> <!-- jQuery code to perform data method --> $("div").data("test", { first: "Geeks", last: "Geeks !" }); $("span:first").text($("div").data("test").first); $("span:last").text($("div").data("test").last); </script> </body> </html> Output: Code #2: In the below code, The data is attaching and retrieving from the "div" element using buttons. html <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/ jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { <!--Here data is attaching to the div element --> $("#b1").click(function() { $("div").data("g", "GeeksforGeeks !!!"); }); <!-- Here data is retrieving from the div element --> $("#b2").click(function() { alert($("div").data("g")); }); }); </script> <style> #b1, #b2 { padding: 10px; margin: 5px; } </style> </head> <body> <!-- This button will attach data to the div element --> <button id="b1">This will attach data to div element</button> <br> <!-- This button retrieve the attached data from the div element --> <button id="b2">This will retrieve the attached data to div element</button> <div></div> </body> </html> Output: Just after clicking the run button- After clicking the "This will retrieve the attached data to div element" button just after clicking the "This will attach data to div element" button- After clicking the "This will retrieve the attached data to div element" button without clicking the "This will attach data to div element" button- Comment More infoAdvertise with us Next Article jQuery | data() with Examples K kundankumarjha Follow Improve Article Tags : JavaScript Web Technologies JQuery jQuery-Misc Similar Reads jQuery | removeData() with Examples 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" wh 1 min read jQuery serializeArray() with Examples The serializeArray() is an inbuilt method in jQuery that is used to create a JavaScript array of objects that is ready to be encoded as a JSON string. It operates on a jQuery collection of forms and/or form controls. The controls can be of several types. JSON string is a text and can convert any Jav 1 min read jQuery event.data Property jQuery event.data property is used to contain the optional data which is passed to an event method. The data is passed when the currently executing handler is bound. Syntax:event.dataParameters:event: It is the parameter that is used to specify the name of the event type on the selector.Example 1: 2 min read jQuery ajax() Method The jQuery ajax() method is used to perform asynchronous HTTP requests, allowing you to load data from a server without reloading the webpage. It provides a flexible way to interact with remote servers using GET, POST, or other HTTP methods, supporting various data formats.Syntax:$.ajax({name:value, 4 min read jQuery ajaxSetup() Method The ajaxSetup() method in jQuery is used to set the default values for future AJAX requests. Syntax: $.ajaxSetup( {name:value, name:value, ... } )Parameters: type: It is used to specify the type of request.url: It is used to specify the URL to send the request to.username: It is used to specify a us 3 min read Like