1

I have a form that displays client-side validations as the user enters information and a jQuery script that gets rid of the error messages after a period of time.

However, the script only works once per page load. I do not know much about jQuery so I am just wondering how I get it to work on every validation message?

It currently works fine for just the first error message:

This is the code verbatim in my rails .js file:

window.setTimeout(function() {
   $(".field_with_errors label").fadeTo(300, 0).slideUp(300, function(){
    $(this).remove(); 
  });
}, 4000);

----Update

Just to be clear: I am using a Rails gem called Client Side Validations that generates new HTML everytime invalid data in entered in the form on this page.

2
  • What do you mean works only once? if you wrap the setTimeout in a named function to call it later it doesn't work? Commented Jun 3, 2013 at 4:44
  • I mean, once the first error message appears, the script removes it, but if another appears on the form from another field, it stays. Commented Jun 3, 2013 at 5:03

1 Answer 1

1

You have to explicitly call your function after every error message appears, so, you could do:

var cleanUpErrorMessages = function() {
    window.setTimeout(function() {
        $(".field_with_errors label").fadeTo(300, 0).slideUp(300, function(){
            $(this).remove(); 
        });
    }, 4000);
}

//...
//A new error appears, then:
cleanUpErrorMessages();

If you are using Client Side Validations take a look at the callbacks in the docs, but I think what you need is:

window.ClientSideValidations.callbacks.element.fail = function(element, message, callback) {
  callback();
  if (element.data('valid') !== false) {
    cleanUpErrorMessages();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.