0

I have a input field with class "kind" and link/button with class "submit".

Now I wanna disable this "kind" input on click of button "submit" if there is no value in input, or if you wanna say, if input is empty...

I tried like this:

html:

<input class="kind" id="name" name="name" type="text" placeholder="Kind">

    <a href="#" class="submit" >Submit</a>

script:

$('.submit').click(function () {
    if ($('.kind').val().length != 0) {
        $('.kind').prop('disabled', true);
    }
});

but it does not work, and I don't know why, you can see here: http://jsfiddle.net/dzorz/UBy4x/

am I missing something? or should I define value check differently..

1
  • $('.kind').val().length == 0 Commented Jun 25, 2013 at 8:33

5 Answers 5

4

I believe you meant to use == instead of != in your conditional statement, if you wanted it to match an empty case.

Also, you should use $.trim to remove any whitespace, otherwise an empty input with spaces won't trigger match the conditional statement.

if ($.trim($('.kind').val()).length === 0){
  $('.kind').prop('disabled', true);
}

jsFiddle here.

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

2 Comments

I think that this will fit my needs
@dzordz No worries, remember to use $.trim like I mentioned, or an empty input with spaces will not disable the button :)
1

Why not simply use .val()

$('.submit').click(function () {
    if (!$('.kind').val()) {
        $('.kind').prop('disabled', true);
    }
});

Fiddle - http://jsfiddle.net/atif089/UBy4x/7/

2 Comments

It is not working. Try to enter some value in textbox then it is show as disable.
@Amit, you are right, I think I used a wrong method here. Updated my answer
0

Your if condition is inverse of what you want from it. You simply have to update your condition. It should be if that value ==0

$('.submit').click( function() {
    if ($('.kind').val().length == 0){
          $('.kind').prop('disabled', true);
    }

});

Comments

0

try this,

$('.submit').click( function() {
    if ($('.kind').val().length < 1){
          $('.kind').prop('disabled', true);
    }

});

Comments

0

You could try this:

$('.kind').attr('disabled', true);

Comments

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.