Codelab Editor
Free Online HTML Editor
Run
Learn jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jQuery Get Data Attributes Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <style> body { font-family: Arial, sans-serif; padding: 30px; background: #f9f9f9; } h2 { color: #222; } #myProduct { margin-top: 20px; padding: 15px; border: 1px solid #ccc; background-color: #fff; } button { margin-top: 20px; padding: 10px 20px; background: #3490dc; color: white; border: none; border-radius: 4px; cursor: pointer; } .info { margin-top: 20px; font-weight: bold; color: #2d3748; } </style> </head> <body> <h2>Get Data Attributes with jQuery <code>.data()</code></h2> <p>This example shows how to retrieve <code>data-id</code> and <code>data-category</code> from a product element using jQuery.</p> <div id="myProduct" data-id="456" data-category="electronics"> Featured Product: Wireless Headphones </div> <button id="getDataBtn">Get Data</button> <div class="info" id="idInfo"></div> <div class="info" id="categoryInfo"></div> <script> $(document).ready(function(){ $('#getDataBtn').click(function(){ var productId = $('#myProduct').data('id'); var productCategory = $('#myProduct').data('category'); $('#idInfo').text("Product ID: " + productId); $('#categoryInfo').text("Category: " + productCategory); }); }); </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