0

I have this code:

setInterval(function(){
  if($("#username_error").val() == "" && $("#password_error").val() == "" && $("#email_error").val() == ""){
    $('input[type="submit"]').removeAttr('disabled');
  } else {
    $('input[type="submit"]').attr('disabled','disabled');
  }
}, 10);

I need to disable the submit button if there are no errors for three divs. When I run this code, nothing happens. But if I do an alert() this if statement runs correctly. What am I doing wrong here?

3
  • 2
    There are many events that you can listen to instead of using setOverkill. Commented Dec 28, 2012 at 11:11
  • try by id for input[type="submit"] and replace setInterval to some other custom function Commented Dec 28, 2012 at 11:12
  • there can be different way than setInterval for this kind of stuff. just a suggation Commented Dec 28, 2012 at 11:27

3 Answers 3

1

Do it like:

$('input[type="submit"]').attr('disabled','disabled');    

$('input[type="text"]').change(function(){
   if($("#username_error").val() == "" && $("#password_error").val() == "" && $("#email_error").val() == "") {
       $('input[type="submit"]').removeAttr('disabled');
   } else {
       $('input[type="submit"]').attr('disabled','disabled');
   }
});

DEMO

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

4 Comments

if error value is empty that means there is no error.... and thn you are making it disable... this makes no sense
@bipen I just gave him the idea of how to do it. I am not considering what these values are. It may be text field or something else.
@MaxPain I have added the demo, you can take a look at that.
ok I used live with hover but the button does not re-enable after there is no errors ? how do I fix this ?
0

Use jQuery keyup event.

$('input').keyup(function(){
       if($("#username_error").val() == "" && $("#password_error").val() == "" && $("#email_error").val() == "") {
         $('input[type="submit"]').attr('disabled',true);
       } else {
         $('input[type="submit"]').attr('disabled', false);
       }
    });

Comments

0

Another solution...

$(document).ready(function () {
    $('button').attr("disabled", true); // starts disabling the button
    $('input').on("blur", null, function () {
        if ($("#username_error").val() != "" && $("#password_error").val() != "" && $("#email_error").val() != "") {
            $("#botao").attr("disabled", false);
        } else {
            $("#botao").attr("disabled", true);
        }
    });
});

1 Comment

If "#username_error", "#password_error" and "#email_error" are three divs. Replace "val()" by "html()"

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.