What I want to do is take an input for a zipcode and in jQuery if input#zip has 5 characters then function. Also same for a list box when the user chooses one of the choices, might be simpler?
A:
Perhaps use the .change(). Each time change is fired get the length of the input and, if its 5 or more, run your code.
SquidScareMe
2010-08-30 19:09:11
+3
A:
$('#zip-input').keyup(function(){
if($(this).val().length == 5) {
//do your stuff here
}
})
Moin Zaman
2010-08-30 19:09:45
Thanks, this is perfect!
sway
2010-08-30 20:20:40
+2
A:
For your zip scenario:
$("#zip").keypress(function() {
if ($(this).val() && $(this).val().length == 5) {
someFunction($(this).val());
}
});
For your listbox scenario:
$("#listbox").change(function() {
if ($(this).val()) {
someFunction($(this).val());
}
});
villecoder
2010-08-30 19:10:47
+2
A:
You might want to use a keyup
event handler for instance.
$('input').bind('keyup', function(){
if($(this).val().length >= 5){
alert('5 characters');
return false;
}
});
jAndy
2010-08-30 19:11:05