user5251319
user5251319

Reputation:

jQuery validator errorplacement

My jQuery has the following code:

highlight: function(element, errorClass, validClass) {
            $(element).addClass(errorClass).addClass('errorImage').removeClass(validClass).removeClass('validImage');
            if($(element.form).find("input[type=radio") == true) {
                $(element.form).find("input[id=" + element.id + "]").addClass(errorClass).addClass('errorImageRadio');  
            }else{
                $(element.form).find("input[id=" + element.id + "]").addClass(errorClass).addClass('errorImage');
            }
            },

What I am trying is, if the input type is radio or checkbox, it should basically use a different class, I am missing something here, not sure what is that.

It is a part of jQuery validator plugin .

Upvotes: 0

Views: 62

Answers (1)

George
George

Reputation: 36784

jQuery objects are always truthy, if you want to test whether the jQuery objects matches any DOM elements, use the .length property.

You also have a missing square bracket in your .find() query:

Instead of

if($(element.form).find("input[type=radio") == true)

Try:

if($(element.form).find("input[type=radio]").length)

For what it's worth (quite a lot) you should use the ID selector to target elements by their ID, not the attribute selector. Since you already have the DOM element in a variable, you can just wrap that in a jQuery object:

$(element.form).find("input[id=" + element.id + "]").addClas...

Becomes

$(element).addClas...

Etc...

Upvotes: 1

Related Questions