Codelab Editor
Free Online HTML Editor
Run
Learn jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jQuery attr() 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: #f4f4f4; } .updated-class { color: #fff; background: #28a745; padding: 10px; border-radius: 4px; } button { margin-top: 20px; padding: 8px 15px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } table { margin-top: 30px; border-collapse: collapse; width: 100%; max-width: 600px; background: #fff; } th, td { border: 1px solid #ccc; padding: 10px; text-align: left; } th { background-color: #eee; } </style> </head> <body> <h2 id="originalId" class="original-class">Heading with Original Attributes</h2> <button id="setAttrBtn">Update ID and Class</button> <table> <tr> <th>Attribute</th> <th>Old Value</th> <th>New Value</th> </tr> <tr> <td>ID</td> <td id="oldIdText"></td> <td id="newIdText"></td> </tr> <tr> <td>Class</td> <td id="oldClassText"></td> <td id="newClassText"></td> </tr> </table> <script> $(document).ready(function(){ const $el = $('#originalId'); // Display original attributes $('#oldIdText').text($el.attr('id')); $('#oldClassText').text($el.attr('class')); $('#setAttrBtn').click(function(){ // Update attributes directly (no callback) $el.attr('id', 'newId'); $el.attr('class', 'updated-class'); // Update table with new values $('#newIdText').text($el.attr('id')); $('#newClassText').text($el.attr('class')); }); }); </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