JQuery validation - how to set the title attribute to the error message

Posted by JK on Stack Overflow See other posts from Stack Overflow or by JK
Published on 2010-06-01T03:40:55Z Indexed on 2010/06/01 3:53 UTC
Read the original article Hit count: 250

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());
        }
    }
});

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-plugins