Link to home
Create AccountLog in
Avatar of Neil Bradley
Neil BradleyFlag for New Zealand

asked on

Enale/disable a button

The fiddle shows 3 input fields and a disabled button.

When entering a value into any field the button's disabled attribute is removed.

Unfortunately, the button becomes disabled again after clearing data from any field.

I'd like to improve the code so that the button's disabled state only reapplies when all fields are empty.

Avatar of skullnobrains

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Neil Bradley


Thank you! Your suggestion worked perfectly

Alternative without an if and with the more useful "input" event that can handle cut and paste

$(function() {
  const $fields = $("._fields-input input")
    .on("input", function() {
        $fields.filter(function() {
          return this.value.trim()
        }).length === 0);

Open in new window