skip to Main Content

So if someone needs to disable a submit button unless the user input is filled, he needs to do this:

        $(function () {
            $('#login').attr('disabled', true);
            $('#userinput').change(function () {
                if ($('#userinput').val() != '') {
                    $('#login').attr('disabled', false);
                } else {
                    $('#login').attr('disabled', true);
                }
            });
        });

And this is the html:

<input type="text" name="userinput" class="form-control" id="userinput">

<button id="login" class="button" disabled="disabled">Submit</button>

And this will work fine but the only problem exists, is that, the user MUST leave the input field in order to run the change event of Javascript.

However I need to run this when user is still active on the input.

So how to do that in Javascript?

2

Answers


  1. use the onfocus eventlistener:

    $('#userinput').focus(func);
    
    Login or Signup to reply.
  2. To track changes before the user leaves the input use the keyup event instead of the change even.

    $('#userinput').on('keyup', function () {
        if ($('#userinput').val() != '') {
            $('#login').attr('disabled', false);
        } else {
            $('#login').attr('disabled', true);
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search