When using jquery .change on an input the event will only be fired when the input loses focus
In my case, I need to make a call to the service (check if value is valid) as soon as the input value is changed. How could I accomplish this?
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.
UPDATED for clarification and example
examples: http://jsfiddle.net/pxfunc/5kpeJ/
Method 1.
inputeventIn modern browsers use the
inputevent. This event will fire when the user is typing into a text field, pasting, undoing, basically anytime the value changed from one value to another.In jQuery do that like this
starting with jQuery 1.7, replace
bindwithon:Method 2.
keyupeventFor older browsers use the
keyupevent (this will fire once a key on the keyboard has been released, this event can give a sort of false positive because when “w” is released the input value is changed and thekeyupevent fires, but also when the “shift” key is released thekeyupevent fires but no change has been made to the input.). Also this method doesn’t fire if the user right-clicks and pastes from the context menu:Method 3. Timer (
setIntervalorsetTimeout)To get around the limitations of
keyupyou can set a timer to periodically check the value of the input to determine a change in value. You can usesetIntervalorsetTimeoutto do this timer check. See the marked answer on this SO question: jQuery textbox change event or see the fiddle for a working example usingfocusandblurevents to start and stop the timer for a specific input field