3

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?

2 Answers 2

10

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();
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is true. It is also true for all the other methods built into the jQuery Validate plugin.
4

Try this instead:

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

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.