0

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');
  }
});

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Possible duplicate of [Detecting data changes in forms using jQuery](https://stackoverflow.com/questions/807159/detecting-data-changes-in-forms-using-jquery) – Ali Sheikhpour Oct 20 '19 at 14:52

2 Answers2

2

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

$(":input").on('edit change').function() {
  // code here
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

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
});
Anis R.
  • 6,656
  • 2
  • 15
  • 37