Form Events
Handle form submission and input events
JavaScript Form Events Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Form submission
const form = document.getElementById('myForm');
form.addEventListener('submit', function(e) {
e.preventDefault(); // Prevent default submission
console.log('Form submitted');
const formData = new FormData(form);
console.log('Form data:', Object.fromEntries(formData));
});
// Method 2: Input events
const input = document.getElementById('myInput');
input.addEventListener('input', function(e) {
console.log('Input value:', e.target.value);
});
input.addEventListener('change', function(e) {
console.log('Value changed:', e.target.value);
});
input.addEventListener('focus', function(e) {
console.log('Input focused');
e.target.style.borderColor = 'blue';
});
input.addEventListener('blur', function(e) {
console.log('Input blurred');
e.target.style.borderColor = '';
// Validate on blur
validateInput(e.target);
});
// Method 3: Real-time validation
input.addEventListener('input', function(e) {
const value = e.target.value;
if (value.length < 3) {
e.target.setCustomValidity('Must be at least 3 characters');
} else {
e.target.setCustomValidity('');
}
});
// Method 4: Select element
const select = document.getElementById('mySelect');
select.addEventListener('change', function(e) {
console.log('Selected:', e.target.value);
});
// Method 5: Checkbox
const checkbox = document.getElementById('myCheckbox');
checkbox.addEventListener('change', function(e) {
console.log('Checked:', e.target.checked);
});
// Method 6: Radio buttons
const radios = document.querySelectorAll('input[type="radio"]');
radios.forEach(radio => {
radio.addEventListener('change', function(e) {
if (e.target.checked) {
console.log('Selected:', e.target.value);
}
});
});
// Method 7: Textarea
const textarea = document.getElementById('myTextarea');
textarea.addEventListener('input', function(e) {
const length = e.target.value.length;
console.log('Character count:', length);
if (length > 100) {
e.target.style.color = 'red';
} else {
e.target.style.color = '';
}
});
// Method 8: Form validation
form.addEventListener('submit', function(e) {
e.preventDefault();
const email = form.querySelector('#email').value;
const password = form.querySelector('#password').value;
if (!email.includes('@')) {
alert('Invalid email');
return;
}
if (password.length < 8) {
alert('Password must be at least 8 characters');
return;
}
console.log('Form is valid');
// Submit form
});
// Method 9: Reset event
form.addEventListener('reset', function(e) {
console.log('Form reset');
});
// Method 10: Invalid event
input.addEventListener('invalid', function(e) {
e.preventDefault();
console.log('Invalid input');
showError(e.target, 'Please enter a valid value');
});// Output depends on form interaction
Understanding Form Events
Form events handle user input and submission.
Event Types
Form Elements
Validation
Best Practices
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Form Events
This JavaScript program is part of the "Events Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.