Petar Brković
Petar Brković

Reputation: 111

Change event to check change on any form element

I have a from with disabled button, which gets enabled when the form is valid. With the function below I'm checking for changes and it works if the form consists only of inputs. How can I check for change on other elements like select or checkbox?

$("#create-rule-form").parsley();
$('input').on('keyup', function() {
  $("#create-rule-form").parsley().validate();
  if ($("#create-rule-form").parsley().isValid()) {
    $('#create-rule-btn').prop('disabled', false);
  } else {
    $('#create-rule-btn').prop('disabled', 'disabled');
  }
});

Upvotes: 0

Views: 1748

Answers (2)

Anis R.
Anis R.

Reputation: 6902

You can use $('form').find('*') to select all of the form's children and grandchildren, and then apply the event to all of them like below.

Also, on a side note, I believe the event you should handle is change instead of keyup, as keyup will not work with checkboxes and dropdowns.

$('form').find('*').on('change', function() {
  //do stuff
});

Upvotes: 0

Barmar
Barmar

Reputation: 780635

Use jQuery's :input selector, it matches all tags that are used for form input.

$(":input").on('edit change').function() {
  // code here
});

Upvotes: 2

Related Questions