jQuery data() Method

jQuery.data() method is really useful for associating various objects, strings, arrays, etc with a DOM nodes and JavaScript objects . Basically jQuery holds the information you store/retrieve with data(name, value)/data(name) in an internal javascript object named cache . It can store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements. When you use the data method , you need to pass two parameters - a key and a value to be stored. The key has to be a string, and the value can be any data structure, including functions, arrays and objects.
$("p").data("str", "You can retrieve this data later"); alert($("p").data("str"));
run this source code Browser View

Full Source
<html> <head> <title>jQuery data() example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#bt1").click(function(){ $("p").data("str", "You can retrieve this data later"); }); $("#bt2").click(function(){ alert($("p").data("str")); }); }); </script> </head> <body> <button id="bt1">Store Data</button><br> <button id="bt2">Get data</button> <p></p> </body> </html>