AKor
AKor

Reputation: 8892

Returning a element's id using its name with jQuery

I'm making a custom jQuery validation method:

        jQuery.validator.addMethod("workaround", function(input, element) {
            if (!($(/*asdf*/).hasClass("intra-field-label"))) 
            {
                return true;
            } 
        }, "This is required");

input is the 'name' of the element. I am trying to get the 'id' of the element, or if I can simply check for the presence of a class using only a 'name', then I want to do that.

Upvotes: 0

Views: 738

Answers (3)

Anthony
Anthony

Reputation: 536

Box9 is correct, the first parameter is the value, the second is the element containing the value. As you know you can also get the value by element.value

 $.validator.addMethod("ValidateAdUser", function (value, element)
        {
        }

Upvotes: 0

David Tang
David Tang

Reputation: 93684

Actually, I don't think the first argument, input, is the name of the element. It should be the value the input currently holds, according to the example given in the documentation.

The second argument is the element that is being validated. Therefore you can simply check:

if ($(element).hasClass('intra-field-label'))

Upvotes: 1

Marko
Marko

Reputation: 72242

jQuery.validator.addMethod("workaround", function(input, element) {
        if (!($("input[name=" + input + "]").hasClass("intra-field-label"))) 
        {
            return true;
        } 
    }, "This is required");

Considering that your element will always be an <input />.

Otherwise change to $("[name=" + input + "]")

Upvotes: 1

Related Questions