2

I have an HTML form where submit button is disabled when input value is less than 10. The button changes its color when input value becomes greater than 10.

Problem comes when I use backspace or delete button to remove input value, submit button color does not change to disabled button until I refresh the page.

setInterval(function () {
  if ($('#tot2').val() >= 10){
    $("#submit").removeAttr("disabled");
    $("#submit").css({"background-color": "blue", "color": "white"});
  } else {
    $("#submit").attr("disabled", "disabled");
  }
}, 10);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="add_form" action="ebill.php" method="post" >
  <input type="text" name="balance"  id="tot2" value=""  />
  <input type="submit" id="submit" name="submit" disabled="disabled" />
</form>
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
achal naskar
  • 710
  • 1
  • 6
  • 19

5 Answers5

2

You change the colors once the value reaches 10, but you never change them back. You can set them to an empty string ("") to get back to the original colors before you set them. (See jQuery - remove style added with .css() function).

Fixed code below:

setInterval(function () {
  if ($('#tot2').val() >= 10){
    $("#submit").removeAttr("disabled");
    $("#submit").css({"background-color": "blue", "color": "white"});
  } else {
    $("#submit").attr("disabled", "disabled");
    // Remove the custom colors we added.
    $('#submit').css({"background-color": "", "color": ""});
  }
}, 10);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="add_form" action="ebill.php" method="post" >
  <input type="text" name="balance"  id="tot2" value=""  />
  <input type="submit" id="submit" name="submit" disabled="disabled" />
</form>

(Note that, as others point out, it's better to monitor the input for changes rather than use a timer.)

user94559
  • 59,196
  • 6
  • 103
  • 103
1

Here you go with a solution

$('#tot2').keyup(function(){
  if(parseInt($(this).val()) < 10 || $(this).val().length  === 0) {
    $('#submit')
      .attr('disabled', 'disabled')
      .removeClass('active');
  } else {
    $('#submit')
      .removeAttr('disabled')
      .addClass('active');
  }
});
.active {
  background: black;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="add_form" action="ebill.php" method="post" >
  <input type="text" name="balance"  id="tot2" value=""  />
  <input type="submit" id="submit" name="submit" disabled="disabled" />
</form>
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
1

Try keyup event. Use class for styling and toggle it.

 $("#tot2").on("keyup", function() {
    var elem = $(this);
    if (elem.val() >= 10) {
        $("#submit").removeAttr("disabled").addClass("active");
    } else {
        $("#submit").attr("disabled", "disabled").removeClass("active");
    }
});

CSS:

active {
        background-color: blue;
        color: white
    }

Demo: http://jsfiddle.net/GCu2D/2207/

K K
  • 17,794
  • 4
  • 30
  • 39
1

You should use keyup or keypress function, and instead of set inline style use addClass like this:

$('#tot2').keyup(function() {
  var val = $(this).val();
  if (val >= 10) {
    $("#submit").removeAttr("disabled");
    $("#submit").addClass('NewSub');
  } else {
    $("#submit").attr("disabled", "disabled");
    $("#submit").removeClass('NewSub');  
  }
});
.NewSub {
background: blue;
color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="add_form" action="ebill.php" method="post">
  <input type="text" name="balance" id="tot2" value="" />
  <input type="submit" id="submit" name="submit" disabled="disabled" />
</form>
Pedram
  • 15,766
  • 10
  • 44
  • 73
-1

Rather than using time interval, you can simply do it like this:

$('#tot2').on('change', function(){
  if($('#tot2').val() < 10) 
    $('#submit').attr('disabled', 'disabled');
  else  $('#submit').removeAttr('disabled');
});
Pedram
  • 15,766
  • 10
  • 44
  • 73
Edison D'souza
  • 4,551
  • 5
  • 28
  • 39