Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JavaScript Form Example</title> </head> <body> <form name="loginForm"> <input type="text" name="username" placeholder="Username"> <input type="password" name="password" placeholder="Password"> <input type="submit" value="Login"> </form> <div style="margin-top:15px;" id="output"></div> <script> var form = document.forms['loginForm']; var username = form.elements['username']; var password = form.elements['password']; form.onsubmit = function() { var output = document.getElementById('output'); output.textContent = 'Username: ' + username.value + ' Password: ' + password.value; return false; // Prevent form submission } </script> </body> </html>