2

It works - I can't click the button when input #key1 is empty. But I need to use two inputs - #key1 and #key2. To do - When both are empty or 1/2, nobody can't click the button. Thanks!!

<script type='text/javascript' src='http://code.jquery.com/jquery.min.js'></script>
<script type='text/javascript'>
  $(function() {
    $('#key1').keyup(function() {
      if ($(this).val() == '') {
        //Check to see if there is any text entered
        // If there is no text within the input ten disable the button
        $('.enableOnInput').prop('disabled', true);
      } else {
        //If there is text in the input, then enable the button
        $('.enableOnInput').prop('disabled', false);
      }
    });
  });
</script>
1
  • can you us your html Commented May 17, 2019 at 13:08

2 Answers 2

1

Try this

$(function() {
  $("#key1, #key2").keyup(function() {
    if ($("#key1").val() == "" || $("#key2").val() == "") {
      $(".enableOnInput").prop("disabled", true);
    } else {
      $(".enableOnInput").prop("disabled", false);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="key1">
<input type="text" id="key2">
<button class="enableOnInput" disabled>Click me</button>

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

1 Comment

Exactly this. Great ;) Thanks !
0

Not sure that I've understood but tell me

<script type='text/javascript'>
  $(function() {
    var button1 = $('#key1');
    var button2 = $('#key2');

    function keyUp(){
      console.log('1', button1.val());
      console.log('2', button2.val());

      var hasValue = ( button1.val() !== '' || button1.val() !== '');
      $('.enableOnInput').prop('disabled', hasValue);
    }

    button1.keyup(keyUp);
    button2.keyup(keyUp);
  });
</script>

2 Comments

Unfortunately, button is disabled. All of time.
I've added console.log to be sure what you get from jQuery selector. Then I also reverse the condition, I was wrong

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.