sikas
sikas

Reputation: 5523

single javascript function for multiple forms

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.

Upvotes: 0

Views: 543

Answers (1)

Šime Vidas
Šime Vidas

Reputation: 186103

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.

Upvotes: 1

Related Questions