Codelab Editor
Free Online HTML Editor
Run
Learn jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jQuery val() - Callback</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: #f9f9f9; } input { padding: 8px; width: 250px; margin-bottom: 10px; display: block; border: 1px solid #ccc; border-radius: 4px; } button { margin-top: 10px; padding: 10px 15px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } button:hover { background: #0056b3; } table { margin-top: 30px; border-collapse: collapse; width: 100%; max-width: 600px; background: #fff; border: 1px solid #ccc; } th, td { padding: 10px; border: 1px solid #ccc; text-align: left; } th { background-color: #f1f1f1; } </style> </head> <body> <h2>Update Inputs Using Callback Logic</h2> <label>Username:</label> <input type="text" id="username" value="JohnDoe"> <label>Email:</label> <input type="text" id="email" value="john@example.com"> <label>Phone:</label> <input type="text" id="phone" value="03001234567"> <button id="updateInputs">Update Fields</button> <table> <tr> <th>Username</th> <th>Email</th> <th>Phone</th> </tr> <tr> <td id="outUsername"></td> <td id="outEmail"></td> <td id="outPhone"></td> </tr> </table> <script> $(document).ready(function(){ // Show initial data $("#outUsername").text($("#username").val()); $("#outEmail").text($("#email").val()); $("#outPhone").text($("#phone").val()); $("#updateInputs").click(function(){ $("input[type='text']").val(function(index, currentValue){ const id = $(this).attr("id"); if(id === "username") { return currentValue.toLowerCase(); } if(id === "email") { const [user, domain] = currentValue.split("@"); return user + "@****.com"; } if(id === "phone") { return currentValue.replace(/^(d{4})(d{3})(d{4})$/, "$1-$2-$3"); } return currentValue; }); // Update table $("#outUsername").text($("#username").val()); $("#outEmail").text($("#email").val()); $("#outPhone").text($("#phone").val()); }); }); </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