0

I have a form on my html page which has name="submitform" and id="submitform"

I want to submit it using Javascript with the following script on that page:

document.forms("submitform").submit();

My submit button in the form has name="btnSubmit" so it doesn't override the original function.

However, it doesn't work

I get: [Error] TypeError: '[object HTMLCollection]' is not a function (evaluating 'document.forms("submitform")'

What to do?

Thanks!

UPDATE - Full Code

 <form action='/post' name='submitform' id="submitform" method='post' class='pure-form'>

                    <textarea columns="40" rows="4" name='entry[body]' id="statement" placeholder='enter a note here to visualize the words and their connections, you can also use #hashtags and @mentions.'><% if (url) { %><%= urltitle %> <%= url %><% } %></textarea>
                    <div id="addToContextsLabel">contexts:</div>
                    <ul id="addToContexts"></ul>
                    <input type="hidden" id="addedContexts" name="addedContexts">
                    <input type="hidden" id="context" name="context" value="<%= context %>">
                    <input type="hidden" id="selectedContexts" name="selectedContexts" value="">
                    <input type="hidden" name="statementid" value="">
                    &nbsp;<br>
                    <input type='submit' id="submitbutton" name="btnSubmit" value="save" class="pure-button pure-button-primary">

            </form>

and

document.submitform.submit();

on the page

0

2 Answers 2

2

Use

document.forms['submitform'] 

instead of round braces. The Background is, that document.forms is an array type and needs to be treated as such.

You can either use the index accessor

document.forms[0]

or by name as i mentioned above.

Hope that helped!

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

1 Comment

I know it's easy to miss: ( vs [
1

you must submit form like this

document.forms["name of your form"].submit();

Comments

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.