spice
spice

Reputation: 1510

Clearing multiple forms with jQuery / JS

I've got a resetForm() function that performs a bunch of tasks on quite a complicated set of forms.

Part of that function is clearing 3 separate forms on reset :

$('form#s1').each(function() { this.reset() });
$('form#s2').each(function() { this.reset() });
$('form#s3').each(function() { this.reset() });

This works fine, but while trying to slim down my code I noticed that this fails if I try to select all 3 forms at once :

$('form#s1','form#s2','form#s3').each(function() { this.reset() });

Am I doing this wrong?

Upvotes: 0

Views: 37

Answers (2)

Therichpost
Therichpost

Reputation: 1815

you can try like this:

 $("form").trigger("reset");

Hope this helps you.

Upvotes: 1

Sabin Adams
Sabin Adams

Reputation: 74

You should be able to do it like this:

$('form#s1,form#s2,form#s3').each(function() { this.reset() });

Note that the value of the selector is all one string in CSV format. Here it is in action.

https://codepen.io/anon/pen/ELwYva

Upvotes: 0

Related Questions