Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>data()</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <div id="box" data-user-id="u123">Box</div> <p> <button id="read">read data-*</button> <button id="write">write $.data()</button> </p> <pre id="out"></pre> <script> $("#read").on("click", function(){ $("#out").text(JSON.stringify($("#box").data(), null, 2)); }); $("#write").on("click", function(){ $("#box").data("role", "admin"); $("#out").text("role=" + $("#box").data("role")); }); </script> </body> </html>
Output