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

asked on

Enale/disable a button

The fiddle https://jsfiddle.net/Takeflite/qw51ydk3/4/ 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.

ASKER CERTIFIED SOLUTION
Avatar of skullnobrains
skullnobrains

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

ASKER

Thank you! Your suggestion worked perfectly

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

https://jsfiddle.net/mplungjan/czf3pug9/

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

Open in new window


If