Codelab Editor
Free Online HTML Editor
Run
Learn jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jQuery data() Example – Set Custom Data Attributes</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <style> body { font-family: sans-serif; padding: 30px; background: #f0f0f0; } .product-box { background: #fff; padding: 20px; border: 1px solid #ccc; max-width: 400px; margin-bottom: 20px; } button { padding: 8px 15px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } table { margin-top: 20px; border-collapse: collapse; width: 100%; max-width: 500px; background: #fff; border: 1px solid #ccc; } th, td { padding: 10px; border: 1px solid #ccc; text-align: left; } th { background-color: #f1f1f1; } </style> </head> <body> <div id="myProduct" class="product-box"> <strong>Product:</strong> Wireless Headphones </div> <button id="setDataBtn">Set Product Data</button> <table> <tr> <th>Key</th> <th>Value</th> </tr> <tr> <td>id</td> <td id="dataId">–</td> </tr> <tr> <td>category</td> <td id="dataCategory">–</td> </tr> </table> <script> $(document).ready(function(){ $('#setDataBtn').click(function(){ $('#myProduct').data('id', 123); $('#myProduct').data('category', 'Electronics'); // Show updated data in the table $('#dataId').text($('#myProduct').data('id')); $('#dataCategory').text($('#myProduct').data('category')); }); }); </script> </body> </html>
Editor Commands
Ctrl
F
Find
Ctrl
G
Find Next
Ctrl
⇧
G
Find Previous
Ctrl
⇧
F
Find & Replace
Ctrl
]
Indent Code Right
Ctrl
[
Indent Code Left
⇧
Tab
Auto Indent Code
Ctrl
/
Line Comment
Ctrl
Alt
/
Block Comment
TutsInsider Actions
Alt
S
Run Code
Ctrl
C
Copy Editor Code
Ctrl
A
Select All Editor Code
Ctrl
S
Save/Download
Ctrl
Alt
X
Rotate
Ctrl
Alt
T
Switch Theme
Ctrl
⇧
C
Open Console
Esc
Close Console