jeewan
jeewan

Reputation: 1605

jQuery enable/disble button depending on input field empty or not

HTML


<input type="text" id="customerProfileID" maxlength="9"/>

<input id="attachTest" class="attachTemplateButton" type="button" value="Attach Template" onclick="attachTemplateCall();"/>

jQuery


$("input").focusin(function() {

        $(this).keydown(function() {
            $("#attachTest").attr("disabled", true);
        });     
    });

I have this code in jsfiddle too HERE I was trying to make the button back to enable state if the input field is empty. That means, the button should be enable if input is empty and should be disabled if the input have some value. The code works only when there is value, the button is disabled but when I erase the entered value, the button still stays in disabled state. What am I missing here?

Upvotes: 4

Views: 4753

Answers (2)

Thomas
Thomas

Reputation: 8859

$("#customerProfileID").on('keyup blur', function(){
    $('#attachTest').prop('disabled', this.value.trim().length);
});

Upvotes: 4

adeneo
adeneo

Reputation: 318342

$("input").on('keyup', function() {
     $("#attachTest").prop("disabled", this.value.length);
});

FIDDLE

Upvotes: 6

Related Questions