5

I have a button thats currently disabled. Once any changes have been made to the inputs in the form. I'd like to enable it....

$('input[class="input-text"]').on('change', (event) => {
    event.preventDefault();
    $('#checkout').removeAttr('disabled');
});

 ...
<button type="submit" className="btn" id="checkout" disabled>Checkout</button>

This doesn't seem to work. What am I doing wrong? (using .jsx syntax)

2 Answers 2

8

Attach input and change both event on a <form>.

Check the following example.

$(document).ready(function() {
  $('#form').on('input change', function() {
    $('#checkout').attr('disabled', false);
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">
  Text:
  <input type="text" class="input-text" />
  <br>Radio:
  <input type="radio">
  <br>Checkbox:
  <input type="checkbox">
  <br>Select:
  <select name="" id="">
    <br>
    <option value="">1</option>
    <option value="">2</option>
    <option value="">3</option>
  </select>
  <button type="submit" className="btn" id="checkout" disabled>Checkout</button>
</form>

Sign up to request clarification or add additional context in comments.

Comments

2

Since you're using jQuery, just use the attr() function (and class selector for more efficient lookup):

$('input.input-text').on('change', (event) => {
    event.preventDefault();
    $('#checkout').attr('disabled', false);
});

jsFiddle Demo

Please note: the change event is only triggered when the .input-text is blurred. You may wish to consider using onkeyup (or similar).

4 Comments

Hmm while I think keyup makes sense. It would still be enabled if the user decided to remove anything that they added.
Yep, that's true. Well, the code above will achieve what you need.
also function(e) will not work in jsx without an expression name
Then it's probably worthwhile to mention in the question that you're using a preprocessor. Either way, the above code achieves what you're looking for, even if the demonstrated syntax is slightly different (or needs modifying to exactly suit your reqs).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.