ST80
ST80

Reputation: 3903

how to check if input field has filled out and radio button has been selected

I want to check if my input field has been filled out and one of my radio buttons has been selected. I tried the following:

$('input').keyup(function() {
    var empty = false;
    if ($('input[type="text"]').val() == '' && $(!$('input[type="radio"]:checked').val())) {
        empty = true;
    }
    if (empty) {
        $('button').attr('disabled', 'disabled');
    } else {
        $('button').removeAttr('disabled');
    }
});

But it doesn't work e.g. it only checks the input-field. can someone tell me what I'm doing wrong?

Upvotes: 2

Views: 596

Answers (2)

user3706295
user3706295

Reputation: 37

It should work

$('input').keyup(function () {
      var empty = false;
      if ($('input[type="text"]').val() == '' && $('#radio_button').is(':checked')) {
             empty = true;
       }

       if (empty) {
            $('button').attr('disabled', 'disabled');
        } else {
            $('button').removeAttr('disabled');
        }
 });

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388416

The keyup event won't be triggered on the radio select

$('input').on('keyup change', function() {
  $('button').prop('disabled', $('input[type="text"]').val() == '' || !$('input[type="radio"]').is(':checked'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button disabled>Save</button>
<input type="text" />
<input type="radio" />

Upvotes: 2

Related Questions