Overview
This tutorial demonstrates how to dynamically enable a submit button only when at least one form field has been filled. This pattern is useful for:
- Search forms with multiple optional filters
- Advanced filters with “search with any criteria” logic
- Auto-save features triggered by any field change
HTML Form Structure
Let’s create a simple form with multiple input types:
<form class="cc-form"> <input type="email" id="email" class="email" name="email" placeholder="Email"> <input type="text" id="name" class="name" name="name" placeholder="Name"> <input type="text" id="city" class="city" name="city" placeholder="City"> <select name="dropdown" id="dropdown" class="dropdown"> <option value="" selected>Choose...</option> <option value="1">Option 1</option> <option value="2">Option 2</option> </select> <input name="checkbox" type="checkbox" value="true"> <button type="submit" id="search" disabled>Search</button></form>Note that the button starts disabled — we’ll enable it via JavaScript when conditions are met.
jQuery Implementation
Here’s the complete jQuery solution:
$(document).ready(function(){ // Initial validation on page load validateButton();
// Attach event listeners to form inputs $('.cc-form input:text').keyup(validateButton); $('.cc-form select').change(validateButton); $('.cc-form input:checkbox').change(validateButton);});
function validateButton() { let hasValue = false;
// Check text inputs and select dropdowns $('.cc-form input:text, .cc-form select').each(function(){ if ($.trim($(this).val()).length > 0) { hasValue = true; } });
// Check checkboxes $('.cc-form input:checkbox').each(function() { if($(this).filter(':checked').length > 0) { hasValue = true; } });
// Enable or disable button based on validation if(hasValue){ $('#search').removeAttr('disabled'); } else { $('#search').attr('disabled', 'disabled'); }}Note
The logic inverts the common pattern: instead of disabling when invalid, we enable when valid. The button starts disabled and becomes enabled only when at least one field has a value.
How It Works
Page Load ($(document).ready))
When the page loads, validateButton() is called immediately. This ensures the button state is correct even if JavaScript loads after cached form values.
Event Listeners
Three event types are monitored:
.keyup()— Fires every time a character is typed in text inputs.change()— Fires when a dropdown selection changes.change()— Fires when a checkbox is checked/unchecked
Validation Logic
The validateButton() function:
- Starts with
hasValue = false - Loops through all text inputs and selects using
.each() $.trim($(this).val()).length > 0— Checks if the field has content (trim removes whitespace)- If any field has content, sets
hasValue = true - Checks all checkboxes separately using
.filter(':checked') - Enables button if
hasValueis true, disables if false
Tip
The $.trim() function removes leading and trailing whitespace, so a field with only spaces won’t trigger the button.
Complete Working Example
// Alternative: More modern vanilla JavaScript approachdocument.addEventListener('DOMContentLoaded', function() { const form = document.querySelector('.cc-form'); const button = document.querySelector('#search');
function validateButton() { const inputs = form.querySelectorAll('input:not([type="checkbox"]), select'); const checkboxes = form.querySelectorAll('input[type="checkbox"]');
let hasValue = false;
// Check text inputs and selects inputs.forEach(input => { if (input.value.trim().length > 0) { hasValue = true; } });
// Check checkboxes checkboxes.forEach(checkbox => { if (checkbox.checked) { hasValue = true; } });
button.disabled = !hasValue; }
// Listen to all input changes form.addEventListener('input', validateButton); form.addEventListener('change', validateButton);
// Initial validation validateButton();});Testing Your Form
- Open the HTML page in a browser
- The submit button should be disabled initially
- Start typing in any field — the button becomes enabled
- Clear all fields — the button becomes disabled again
- Select a checkbox — the button becomes enabled
Warning
Remember to test all input types: text fields, dropdowns, and checkboxes to ensure the validation works correctly across your form.
Live Demo & Source Code
- Working Demo: Submit Button Demo
- Source Code: GitHub Repository
Summary
This technique provides a smooth user experience by giving visual feedback (enabled/disabled button) based on form state. It works with any combination of input types and is easily customizable for different validation logic.