1

This was working but has suddenly stopped for some reason. I can't see what's wrong. Can someone tell me what I am doing wrong here? I'm using an onclick event in a span tag then calling this function.

Firefox reports: Error: document.forms[0].submit is not a function

 function submitlogin() {
 document.forms[0].submit()
 }
<form method="post" id="submit" action="something.asp">
 <span id="button" onclick="submitlogin()"></span>
</form>

This is what the form looks like

 <form method="post" id="myform" action="">
 <div>
 <input type="text" id="name" name="name" />
 </div>
 <div id="btn-container">
 <span id="button" onclick="submitlogin();"></span>
 </div>
 </form>
asked Nov 16, 2010 at 22:07
4
  • Do you even need to use javascript? Commented Nov 16, 2010 at 22:13
  • Well, I'm using a background image that has a button on it. I've had to use css to align the x and y coords of that button then use javascript to submit the form. Is there a better way? Commented Nov 16, 2010 at 22:16
  • 1
    you can apply the css style to the button instead of replacing it with a span. E.g. see app.portasigma.com/login.jsp where we nicely style a button Commented Nov 16, 2010 at 22:38
  • @Carles, thanks for the link. It will be very helpful, indeed. Always learning something new. :) Commented Nov 16, 2010 at 22:49

1 Answer 1

1

document.forms[0] is searching for a <form> in your code, which you don't have. A quick fix could be

<script type="text/javascript">
 function submitlogin() {
 document.forms["myform"].submit();
 }
</script>
<form method="post" id="myform" action="something.asp">
 <span id="button" onclick="submitlogin()">hello</span>
</form>
answered Nov 16, 2010 at 22:09
Sign up to request clarification or add additional context in comments.

14 Comments

I do have a form tag. It encompasses the span tag
Capt, From inside the function, I can alert the id. I get: [object HTMLSpanElement]. Not sure if this helps
This edit should work, sorry I had a typo before. Notice I changed the id of the form to "myform" rather than "submit". I get paranoid that some browsers will not run the code correctly if you use keywords like that.
I agree with you and was exactly why I changed it too. :) Let me give your edit a try. brb
No Capt. I get this error: Error: document.myform is undefined
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.