I am validating some text inputs using the keyup function, and I want to run an animation of a check mark when the box is considered validated.
The problem is that as soon as I click into any input, the animation starts. If you type once as soon as you click in, you will see the animation happen. I’m assuming the animation starts because i’m adding the class on keyup, which essentially get’s triggered after you release your mouse to click into it. Is there another way around that?
- I increased the time to 5 seconds just while testing it
- Why is it triggering all 3 animations since i’m using the
eachand thenextfunctions. - Is there a way around having the animation start on
keyup.
The gist of my code is below, see a live version here: http://jsfiddle.net/MhMCz/26/
$(':input').each(function(){
var values = $(this).attr('value');
$(this).keyup(function(){
if( $(this).val() !== '' && $(this).val() !== values) {
$(this).addClass('animated');
$(this).next('span').html("<img src='http://png.findicons.com/files/icons/1581/silk/16/tick.png' />");
}
else {
$(this).removeClass('animated');
$(this).next('span').html('');
}
});
if ( $(this).next('span').hasClass('animated') ) { }
else {
$(this).next('span').animate({
marginLeft: '10px'
}, 5000, function(){
alert("animation complete")
});
}
)};
I reconstructured your code a little, could be optimzed more but it looks quite nice:
http://jsfiddle.net/MhMCz/30/
The event delegation from “form” ist just for performance, see http://jsperf.com/jquery-live-vs-delegate-vs-on/4.