views:

565

answers:

3

Hi guys,

I'm working with the jQuery Validation plugin and I wrote the following code which adds a class to the element's (<input>) parent (<label>) if not valid, as well as inserting the actual error element (<span>) before the <br>.

the HTML ...

<label>
    text<br><input>
</label>

... and the jQuery.

$("#form_to_validate").validate({
    rules: {
    ...
    },
    errorElement: "span",
    errorPlacement: function(error, element) {
        element.parent().addClass('error');
        error.insertBefore(element.parent().children("br"));
    }
});

So, if a form element doesn't validate, it turns into:

<label class="error">
    text<span>error text</span><br><input>
</label>

This works great, however, if a field's content is corrected and becomes valid, the class obviously doesn't get removed from its parent (actually, neither does the error element, instead it just gets a display: none; CSS property). How can I check if an element becomes valid and remove its parent class if so ?

Any help would be greatly appreciated !


Edited: Added more information.

A: 

After some more heavy digging I found the answer right under my nose, but unfortunately for me, well hidden. The plug-in has a built-in highlighting function (surprise, surprise). Normally it would highlight/unhighlight the element, but it can easily be converted to work on the element's parent:

$("#form_to_validate").validate({
    rules: {
    ...
    },
    errorElement: "span",
    errorPlacement: function(error, element) {
        error.insertBefore(element.parent().children("br"));
    },
    highlight: function(element) {
        $(element).parent().addClass("error");
    },
    unhighlight: function(element) {
        $(element).parent().removeClass("error");
    }
});

I actually found this by looking into the plug-in's source code (duh!) since its documentation on jquery.com doesn't even mention it, which is really a shame as IMHO it is a pretty important function in terms of user-friendliness and I think it should be at least mentioned in the documentation.

I hope this will help someone and spare them a couple of hours of frustration, and many thanks to everyone who read the question regardless. It's the intention that counts :)

FreekOne
A: 

is there a way to to make for not produce the (<label>) because the error is handling by CSS

GusDe CooL
oh sorry alerady found it by readit it source . just use this(errorElement: "",)
GusDe CooL
Yes, that is exactly what I'm using in the posted answer :) Also, as a friendly advice: you have more chances of actually getting an answer by posting your own question and linking back to the related question if needed. The answer section is meant for answers only, not for follow-up questions as this isn't a forum. I will not downvote you because I see you're new here, but others might not be as tolerant.
FreekOne
yes, my mistakethank for the advice...happy to be here.
GusDe CooL
A: 

errorElement: 'none' worked for me ...

chris