2

I have a search form with 3 inputs (2 texts and one dropdown select), and I made a jQuery pre-validation code that disable the submit button unless something is typed on those inputs. Code looks like this:

// Search button is disabled until something is typed.
$(document).ready(function () {
 $('input[type="submit"]').prop('disabled', true);
 $('input[type="text"]').keyup(function () {
   if ($(this).val() != '') {
      $(':input[type="submit"]').prop('disabled', false);
   } else {
      $(':input[type="submit"]').prop('disabled', true);
   }
});
 $('#dropdown').on("change", function () {
   if ($(this).val() == '') {
      $(':input[type="submit"]').prop('disabled', true);
  } else {
      $(':input[type="submit"]').prop('disabled', false);
  }
 });
});

The code works perfectly on web/desktop. Then, I wanted to personalize the page for mobile devices, so I've added the exactly same form with those 3 inputs into the d-block d-sm-none Bootstrap 4 class - the div will be shown only when user is on mobile.

The code is responding while on mobile, it does its job on first two inputs (text ones), but the dropdown select is not doing the trick. Even if I select something from the list, the button is still disabled.

The dropdown code looks like this in both version (desktop and mobile):

<select class="custom-select" name="status" id="dropdown">
<option selected="selected" value="">Find by status</option>
@foreach($status as $sts)
<option value="{{$sts->id}}">{{$sts->name}}</option>
@endforeach
</select>

Any ideas? Thank you!

4
  • The code works perfectly - does it? From what I can see, if you type something into the textbox, you get the submit... if you select a dropdown you get the submit... but if you remove the textbox value (leaving the dropdown selection) then you'd lose the submit Commented Jun 18, 2019 at 8:28
  • Nicely done. But your eagle eye doesn't resolve my problem :) Commented Jun 18, 2019 at 8:30
  • Indeed, but you'll notice I didn't try and answer your problem, because I don't know the answer Commented Jun 18, 2019 at 8:32
  • Aight. Thanks for the observation tho, it's fixed now! Cheers. Commented Jun 18, 2019 at 8:34

1 Answer 1

3

You need to use class instead of id to select the dropdown :

$(document).ready(function () {
 $('input[type="submit"]').prop('disabled', true);
 $('input[type="text"]').keyup(function () {
   if ($(this).val() != '') {
      $(':input[type="submit"]').prop('disabled', false);
   } else {
      $(':input[type="submit"]').prop('disabled', true);
   }
});
 $('.custom-select').on("change", function () {
   if ($(this).val() == '') {
      $(':input[type="submit"]').prop('disabled', true);
  } else {
      $(':input[type="submit"]').prop('disabled', false);
  }
 });
});

When you use $('#dropdown') your code will be only apply on the first Select in the DOM

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

Comments

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.