0

I need to add a class to div that is wrapped around the select element upon validation.

$("#Form").validate({
    invalidHandler: function (form, validator) {
        $("[id$='_DropDownList']").each(function () {
            // How do I figure out if $(this) is valid or invalid?
            // Add the class below if invalid.
            $(this).parent().addClass("error");
        });
    }
});

3 Answers 3

6

You can use the .valid() method, like this:

$("#Form").validate({
    invalidHandler: function (form, validator) {
        $("[id$='_DropDownList']").each(function () {
            if(!$(this).valid())
              $(this).parent().addClass("error");
        });
    }
});

However, the highlight and unhightlight options are specifically for this:

$("#Form").validate({
  highlight: function(element, errorClass, validClass) {
    $(element).parent().removeClass(validClass).addClass(errorClass);
  },
  unhighlight: function(element, errorClass, validClass) {
    $(element).parent().removeClass(errorClass).addClass(validClass);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like this code is correct:

$(this).parent().addClass("error");

Maybe your issue is with the code surrounding it... Try moving your addClass line outside the function it is in to see if it work there.

Comments

0

invalidHandler is only called when the form is submitted and not valid. I believe adding your code under that function alone is enough.

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.