mamu
mamu

Reputation: 12414

jquery validator validate() and selector

I have two forms in a page

<form id="form1">
</form>
<form id="form2">
</form>

in javascript
$('form').validate();

Above only applies validator to form1 not form2. i thought jquery selector applies to all matched elements.

I had to call validate on individual form to apply

Is something wrong with what i am doing/expecting? isn't $('form').validate() should apply to all forms in a page?

Upvotes: 3

Views: 1238

Answers (2)

joekarl
joekarl

Reputation: 2118

While the jQuery function does grab every matching element for the selector, it is up to the plugin to use those elements. It looks like the validate plugin you are using (can't confirm if it is this one) only grabs the first element of the selector to act upon.

By wrapping the validate function with an each, you should be able to get the validate functionality for each form.

$('form').each(function(){
    $(this).validate();
});

Upvotes: 10

kinakuta
kinakuta

Reputation: 9037

Try this instead:

$('form').each(function(){
    $(this).validate();
});

Upvotes: 4

Related Questions