4

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?

2 Answers 2

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

FIDDLE

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

4 Comments

it should be noted that .prop() should be used now instead of .attr()
Hi Adeneo, how can I check the same for more than two inputs? Can you please check this fiddle again jsfiddle.net/jeewanaryal/cFedv Thanks
@jeewan - for both, it would be something like -> FIDDLE
This one is succinct and beautiful. :)
4
$("#customerProfileID").on('keyup blur', function(){
    $('#attachTest').prop('disabled', this.value.trim().length);
});

1 Comment

I added blur since otherwise the button wouldn't be disabled when pasting with the mouse.

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.