views:

34

answers:

1

In JQuery validation the default behavior on an error is to create a label like so:

<label for="FirstName" generated="true" class="error">This field is required.</label>

Is it possible to change it so that it will output this instead (with a title attribute set to the error message)?

<label for="FirstName" generated="true" class="error" title="This field is required.">This field is required.</label>

I've tried the highlight method, but the label has not been created yet:

$("#form").validate({
    highlight: function (element, errorClass) {
        var label = $("label[for=" + element.id + "]");  // but label doesn't exist yet so this doesnt work
        if (label && label.length > 0) {  // label.length is always 0
            label.attr('title', label.text());
        }
    }
});
+1  A: 

ok try this...

$("#form").validate({
    highlight: function (element, errorClass) {
        setTimeout(function() {
            var label = $("label[for=" + element.id + "]");
            if (label && label.length > 0) { 
                label.attr('title', label.text());
            }
        }, 10);     
    }
});

basically we just delay the part where we set the title attribute for 10ms so we are sure that the label element is created first.

hope this helps

ricecake5
A bit of a hack, but seems to work, trying it out now.
JK
yeah but I think that's the closest we could get to success callback's counterpart...
ricecake5