Forum Moderators: open

Message Too Old, No Replies

Simple Form Submission Event (IE7 problem)

         

vol7ron

7:09 pm on Mar 5, 2010 (gmt 0)

10+ Year Member



[pre]<form action="#" onsubmit="alert('test');">
<input name="foo" type="text" onkeypress="if(event.keyCode==13){form.submit();}" />
<input type="submit" value="Submit" />
</form>[/pre]


This submits the form when a user presses the 'Enter' key in the text box, but it won't call the test message before it continues. Any ideas why?

As always, it works in FF.

I know I could place the 'test' message in the input's keypress event, but I'm curious if there's an explanation or a better way to call the onsubmit.


Thank you,
vol7ron

vol7ron

7:28 pm on Mar 5, 2010 (gmt 0)

10+ Year Member



So apparently, I've known this in the past:

<form action="#" onsubmit="alert('test');">
<input name="foo" type="text" onkeypress="if(event.keyCode==13){[b]document.getElementById('submitbutton').click()[/b];}" />
<input [b]id="submitbutton"[/b] type="submit" value="Submit" />
</form>

Fotiman

7:51 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The onkeypress event handler is not needed. Browsers will automatically submit the form using the first input with type "submit" when the Enter key is pressed while in a text box.