Form Events in JavaScript
Handle form submission and input events
BeginnerEvents ProgramsExample 7 of 10
form-events.js
1// Method 1: Form submission2const form = document.getElementById('myForm');34form.addEventListener('submit', function(e) {5 e.preventDefault(); // Prevent default submission6 console.log('Form submitted');78 const formData = new FormData(form);9 console.log('Form data:', Object.fromEntries(formData));10});1112// Method 2: Input events13const input = document.getElementById('myInput');1415input.addEventListener('input', function(e) {16 console.log('Input value:', e.target.value);17});1819input.addEventListener('change', function(e) {20 console.log('Value changed:', e.target.value);21});2223input.addEventListener('focus', function(e) {24 console.log('Input focused');25 e.target.style.borderColor = 'blue';26});2728input.addEventListener('blur', function(e) {29 console.log('Input blurred');30 e.target.style.borderColor = '';31 // Validate on blur32 validateInput(e.target);33});3435// Method 3: Real-time validation36input.addEventListener('input', function(e) {37 const value = e.target.value;38 if (value.length < 3) {39 e.target.setCustomValidity('Must be at least 3 characters');40 } else {41 e.target.setCustomValidity('');42 }43});4445// Method 4: Select element46const select = document.getElementById('mySelect');4748select.addEventListener('change', function(e) {49 console.log('Selected:', e.target.value);50});5152// Method 5: Checkbox53const checkbox = document.getElementById('myCheckbox');5455checkbox.addEventListener('change', function(e) {56 console.log('Checked:', e.target.checked);57});5859// Method 6: Radio buttons60const radios = document.querySelectorAll('input[type="radio"]');6162radios.forEach(radio => {63 radio.addEventListener('change', function(e) {64 if (e.target.checked) {65 console.log('Selected:', e.target.value);66 }67 });68});6970// Method 7: Textarea71const textarea = document.getElementById('myTextarea');7273textarea.addEventListener('input', function(e) {74 const length = e.target.value.length;75 console.log('Character count:', length);7677 if (length > 100) {78 e.target.style.color = 'red';79 } else {80 e.target.style.color = '';81 }82});8384// Method 8: Form validation85form.addEventListener('submit', function(e) {86 e.preventDefault();8788 const email = form.querySelector('#email').value;89 const password = form.querySelector('#password').value;9091 if (!email.includes('@')) {92 alert('Invalid email');93 return;94 }9596 if (password.length < 8) {97 alert('Password must be at least 8 characters');98 return;99 }100101 console.log('Form is valid');102 // Submit form103});104105// Method 9: Reset event106form.addEventListener('reset', function(e) {107 console.log('Form reset');108});109110// Method 10: Invalid event111input.addEventListener('invalid', function(e) {112 e.preventDefault();113 console.log('Invalid input');114 showError(e.target, 'Please enter a valid value');115});
Output
// Output depends on form interaction
What's going on
Form events handle user input and submission.
Event Types
submit: Form submitted
reset: Form reset
input: Value changed (real-time)
change: Value changed (on blur)
focus: Element focused
blur: Element blurred
invalid: Validation failed
Form Elements
input: Text, email, password, etc.
textarea: Multi-line text
select: Dropdown
checkbox: Boolean input
radio: Single choice
Validation
HTML5 validation
Custom validation
Real-time feedback
Error messages
Best Practices
Prevent default on submit
Validate on blur
Show errors clearly
Use FormData for collection