0

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.

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 You want document.forms and then form.elements. I've updated my answer...
I found a workaround using document.forms[0].elements.length ... Thanks :)

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.