If you want it to act like Google, then you can listen for the submit
event on the form, and then use .preventDefault()
to prevent the form from submitting if the input value is empty. See https://stackoverflow.com/a/8664535/1499877 for another example.
form = document.querySelector('form');
input = document.querySelector('input');
form.addEventListener('submit', function(event) {
if (input.value == '') {
event.preventDefault(); // prevent the form from submitting
}
});
<form>
<input type="search" />
<button>
Submit?
</button>
</form>
Another option with slightly better user experience is to disable the button by default, and then enable it when the text input field has some value. This at least provides the user with a little feedback (the button becomes enabled) when they enter something in the input field.
input = document.querySelector('input');
button = document.querySelector('button');
input.addEventListener('input', function(event) {
if (input.value == '') {
button.disabled = true;
} else {
button.disabled = false;
}
});
<form>
<input type="search" />
<button disabled>
Submit?
</button>
</form>