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>
-
Do you even need to use javascript?Capt Otis– Capt Otis2010年11月16日 22:13:38 +00:00Commented 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?jim– jim2010年11月16日 22:16:04 +00:00Commented Nov 16, 2010 at 22:16
-
1you 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 buttonCarles Barrobés– Carles Barrobés2010年11月16日 22:38:51 +00:00Commented Nov 16, 2010 at 22:38
-
@Carles, thanks for the link. It will be very helpful, indeed. Always learning something new. :)jim– jim2010年11月16日 22:49:03 +00:00Commented Nov 16, 2010 at 22:49
1 Answer 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
Capt Otis
1,2601 gold badge12 silver badges18 bronze badges
Sign up to request clarification or add additional context in comments.
14 Comments
jim
I do have a form tag. It encompasses the span tag
jim
Capt, From inside the function, I can alert the id. I get:
[object HTMLSpanElement]. Not sure if this helpsCapt Otis
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.
jim
I agree with you and was exactly why I changed it too. :) Let me give your edit a try. brb
jim
No Capt. I get this error:
Error: document.myform is undefined |
lang-js