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.
-
Possible duplicate stackoverflow.com/questions/8563299/… stackoverflow.com/questions/6681583/… stackoverflow.com/questions/7057833/…Sameera Thilakasiri– Sameera Thilakasiri2011-12-21 17:53:14 +00:00Commented Dec 21, 2011 at 17:53
-
@SameeraThilakasiri Those are are not valid replacements. The OP doesn't want to submit multiple forms at once...Šime Vidas– Šime Vidas2011-12-21 17:56:50 +00:00Commented Dec 21, 2011 at 17:56
Add a comment
|
1 Answer
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.
3 Comments
sikas
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.
Šime Vidas
@Sikas You want
document.forms and then form.elements. I've updated my answer...sikas
I found a workaround using
document.forms[0].elements.length ... Thanks :)