I have a very simple function here:
$('#input').keydown( function(e) {
if( $(this).length === 8 ) { alert('We have a winner!'); }
else { return false; }
});
It does not work. Why? Any help much appreciated.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming you want to check the length of the value of
#input, then you want:Instead of
$(this).length. You also may want to get rid of thereturn false;as that could prevent anything from being entered into the input field.See an updated fiddle here.
On a separate note, this will only work when there are already 8 characters in the field, and another key is pressed. That may be the way you intended it, but I think you may want the
keyupevent instead.