Aerodynamika
Aerodynamika

Reputation: 8413

Submit a form from html using Javascript?

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

Upvotes: 0

Views: 107

Answers (2)

ashkufaraz
ashkufaraz

Reputation: 5297

you must submit form like this

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

Upvotes: 1

Andreas K&#252;hntopf
Andreas K&#252;hntopf

Reputation: 151

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!

Upvotes: 2

Related Questions