Submit buttons
Part of the JavaScript in Action section of Coddy's HTML journey — lesson 7 of 27.
Submit buttons are essential elements in HTML forms that allow users to send form data to a server for processing.
Create a basic HTML form with a submit button:
<form id="myForm">
<input type="text" placeholder="Enter your name">
<button type="submit">Submit</button>
</form>The type="submit" attribute specifies that the button should submit the form when clicked.
You can also use JavaScript to handle form submissions:
const form = document.getElementById('myForm');
form.addEventListener('submit', function(event) {
event.preventDefault(); // Prevents the default form submission
console.log('Form submitted!'); // This prints a message in the console
});The preventDefault() method stops the form from submitting in the traditional way, allowing you to handle the submission with JavaScript.
Challenge
EasyYour task:
- Add a Submit button to the form with
type="submit" - Inside the submit event listener, use the
messagevariable to set the text content to "Submitted successfully"
Try it yourself
<!DOCTYPE html>
<html>
<head>
<title>Submit buttons</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form id="myForm">
<h2>Sign Up</h2>
<input type="text" id="email" placeholder="Enter your email">
<p id="emailMessage" class="message"></p>
</form>
<script src="script.js"></script>
</body>
</html>
This lesson includes a short quiz. Start the lesson to answer it and track your progress.