Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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?

like image 502
jeewan Avatar asked Dec 27 '22 08:12

jeewan


1 Answers

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

FIDDLE

like image 86
adeneo Avatar answered Feb 06 '23 09:02

adeneo