I have a page that loads different forms depending on user choice. I want to have a single javascript that can read all elements on any of these forms. I don't want to have multiple scripts ... I want a a function, say for example, named submit([don't know if it should have parameters])
, then when any of the forms is submitted, this function is called and executed. I will be setting the submit action. But I need the function that can read any form.
Asked
Active
Viewed 537 times
0

sikas
- 5,435
- 28
- 75
- 120
-
Possible duplicate http://stackoverflow.com/questions/8563299/submit-multiple-forms-with-one-submit-button http://stackoverflow.com/questions/6681583/how-to-submit-multiple-forms-with-single-submit http://stackoverflow.com/questions/7057833/jquery-submit-multiple-forms-through-single-reqest-without-ajax – Sameera Thilakasiri Dec 21 '11 at 17:53
-
@SameeraThilakasiri Those are are not valid replacements. The OP doesn't want to submit multiple forms at once... – Šime Vidas Dec 21 '11 at 17:56
1 Answers
1
Consider this:
document.onsubmit = function ( e ) {
e.preventDefault();
submit( ... );
};
So, you cancel any submit actions on the document-level, and then do your own thing using submit()
...
Use the document.forms
collection to access your forms. Use the form.elements
collection to access the elements of each form.

Šime Vidas
- 182,163
- 62
- 281
- 385
-
This is not what I'm looking for ... I want the function to be able to get all the form elements then I'll do whatever the operation is with the form elements. – sikas Dec 21 '11 at 18:13
-
@Sikas You want `document.forms` and then `form.elements`. I've updated my answer... – Šime Vidas Dec 21 '11 at 18:20
-
I found a workaround using `document.forms[0].elements.length` ... Thanks :) – sikas Dec 21 '11 at 18:28