2

I am trying to submit the parent form, using an "onclick" event without specifying the form's id. There is a reason for this. I am using a webfont for a button hence the "span".

My code:

<form action="/Index" method="post">    <p>
    <span class="fa fa-arrow-circle-right" onclick="document.form.submit();" style="font-size:50px;cursor:pointer;"></span>  
    <input type="hidden" name="id" value="10000"/>     
</p>

I have also tried:

onclick="this.form.submit();"

This also failed to work.

What is the correct code please?

3
  • 1
    What's wrong with a submit button? No script required. Only form controls (input, textarea, etc.) in a form have a form property by default. Commented Nov 21, 2013 at 1:29
  • How can I use a webfont when using a button element. The webfonts are activated by the class attribute. They are the "font awesome" ones. This is why I am using a span tag. Commented Nov 21, 2013 at 1:34
  • RobG, I understand where you are coming from, and most of my forms just use button tags. However this is a special use case where the user needs a webfont. Thanks for the wisdom however. Commented Nov 21, 2013 at 1:46

4 Answers 4

1

You can use document.forms[0].submit() if you have only one form in page. In case if you have multiple forms on the same page then it would submit the first encountered form.

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

1 Comment

The OP could use a button in the form and then this.form.submit() would work. But it would work without the script too. ;-)
0

Is there any other forms?

if no, try: 'document.forms[0].submit'

otherwise, add a id to the form

<form id="testForm" action='..'>...</form>

document.getElementById('testForm').submit();

Comments

0

You didn't close your form element.

<form action="/Index" method="post">    
    <p>
    <span class="fa fa-arrow-circle-right" onclick="document.form.submit();" style="font-size:50px;cursor:pointer;"></span>  
    <input type="hidden" name="id" value="10000"/>     
    </p>
</form> /*This*/

Should work once you close it.

Comments

0

The below strategy to submit the form would work even if have multiple forms on the same page given that you provide a unique id to each form. The <form> should be provided an id attribute so that specific can be targeted with the given id as below:

<form id="myFormId" action="/Index" method="post">
    <p>
    <span onclick="document.getElementById('myFormId').submit()" class="fa fa-arrow-circle-right" role="button" style="font-size:50px;cursor:pointer;"></span>  
    <input type="hidden" name="id" value="10000"/>     
    </p>
</form>

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.