jkilbride
jkilbride

Reputation: 1353

JQuery Examine First Character in Text Field as its typed

I've a simple html form with text field with id="zip" what I want to do is monitor that field and whenever the value changes enable/disable an array of checkboxes based on the input. Currently the change event only fires once I tab or the zip input field. I tried experimenting with keypress but it would always give the value of the text field minus the current character:

 <form id+"testform">
 Zip Code <input id="zip" value="" /><br />
 Zip 4<input type="checkbox" id="check_one" /><br />

 Zip 3<input type="checkbox" id="check_two" /><br />
</form>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>


<script type="text/javascript">
 $('#zip').change(function() {
 var zipCode = $(this).val();
 zipCode = zipCode.substring(0,1);



 if(zipCode == 3) {
  $("#check_one").attr('disabled','disabled');
    $("#check_two").removeAttr('disabled');


 } else if (zipCode == 4) {
  $("#check_one").removeAttr('disabled');
    $("#check_two").attr('disabled','disabled');


 } else {
  $("#check_one").removeAttr('disabled');
  $("#check_two").removeAttr('disabled');

 }
});
</script>

Is there an appropriate jQuery event that fires after each character is input?

Upvotes: 1

Views: 1912

Answers (1)

Macy Abbey
Macy Abbey

Reputation: 3887

onkeyup should be the event you are looking for.

http://api.jquery.com/keyup/

Upvotes: 4

Related Questions