I think the problem is that you only ever remove a node when there is
no errors. Why don't you just remove ALL error messages each call - so
you'll only ever be showing the most recent error:

function(error){
   $('.error-message').remove(); //remove ALL previous error messages
   if(error.length > 0){
      // Add error message
   }
}

On Sep 16, 1:59 am, "Dave Maharaj :: WidePixels.com"
<d...@widepixels.com> wrote:
> I have an ajax function that checks fields to validate and display error
> messages.
>
> There are multiple messages depending on the error per field in some cases,
> to short, to long, already taken, invalid, cant be empty and so on....you
> get the idea.
>
> My function is like this:
>
> function(error) {
>
>                    if(error.length != 0) {  
>
>                        if ( $('#' + fieldName + '-exists').length == 0 )
>         {
>           $('#' + fieldName).after('<div class="error-message" id="'+
> fieldName +'-exists">' + error + '</div>');
>         }
>
>                    }
>                    else {
>                        $('#' + fieldName + '-exists').remove();
>                    }
>                });
>
> But the error never changes once 1 is called so if to short it says too
> short....fix the problem and it only shows too short never any other
> message. How can i remove the message if its fixed but still return
> anothererror if it exists?
>
> I tried if ( $('#' + fieldName + '-exists').length != error )
> so if the error is different display the error but it doesnot remove the
> original error....
>
> Ideas?
>
> Thanks
>
> Dave

Reply via email to