...

View Full Version : onkeypress event to submit a form?



Bob Leny
08-01-2006, 09:30 PM
** SOLVED **


I have this form:


<form action="index.php?mid=72me211v4604" method="POST">
<textarea name="msg"></textarea>
<br>
<input type="submit" value="S&#228;nd/uppdatera">
</form>


Useing <textarea></textarea>, is there a way to make it submite when you hit <Enter>?

With <textarea></textarea>, when you hit <Enter> it jumps down a line. I don't want it to do that. I want the form to submit when the enter key is pressed.

I'm thinking "onkeypress" event. How do I do this? I know nothing about this event.

Thanks!

boeing747fp
08-01-2006, 09:42 PM
<script language="JavaScript">
function submitonEnter(){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if(keycode == "13"){
document.form1.submit();
}
}
</script>
<form name="form1" action="index.php?mid=72me211v4604" method="POST">
<textarea name="msg" onKeyDown="submitonEnter();"></textarea>
<br>
<input type="submit" value="S&#228;nd/uppdatera">
</form>

Bob Leny
08-01-2006, 10:03 PM
<script language="JavaScript">
function submitonEnter(){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if(keycode == "13"){
document.form1.submit();
}
}
</script>
<form name="form1" action="index.php?mid=72me211v4604" method="POST">
<textarea name="msg" onKeyDown="submitonEnter();"></textarea>
<br>
<input type="submit" value="Sänd/uppdatera">
</form>


Thanks! But It seems to only work in IE.

Pyth007
08-02-2006, 12:32 AM
Try changing the first part of this function to:


function submitonEnter(e){
var keycode = (!e)?window.event.keyCode:e.which;

Bob Leny
08-02-2006, 03:30 AM
Try changing the first part of this function to:


function submitonEnter(e){
var keycode = (!e)?window.event.keyCode:e.which;


Nope didnt work... :(

This script works on FF IE and Opera...


<html>
<body>
<script type="text/javascript">
function noNumbers(e)
{
var keynum
var keychar
var numcheck

if(window.event) // IE
{
keynum = e.keyCode
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
numcheck = /\d/
return !numcheck.test(keychar)
}
</script>

<form>
<input type="text" onkeydown="return noNumbers(event)" />
</form>

</html>


But, I don't know how to addapt it. I just don't know any of that. I only know really basic stuff.

boeing747fp
08-02-2006, 04:55 AM
this script here works for me in IE and FF.



<script language="JavaScript">
function submitonEnter(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if(charCode == "13"){
document.form1.submit();
}
}
</script>
<form name="form1" action="index.php?mid=72me211v4604" method="POST">
<textarea onKeyPress="javascript:return false;" name="msg" onKeyDown="javascript:return submitonEnter();"></textarea>
<br>
<input type="submit" value="S&#228;nd/uppdatera">
</form>

glenngv
08-02-2006, 05:33 AM
this script here works for me in IE and FF.



<script language="JavaScript">
function submitonEnter(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if(charCode == "13"){
document.form1.submit();
}
}
</script>
<form name="form1" action="index.php?mid=72me211v4604" method="POST">
<textarea onKeyPress="javascript:return false;" name="msg" onKeyDown="javascript:return submitonEnter();"></textarea>
<br>
<input type="submit" value="Sänd/uppdatera">
</form>

Are you sure it works in Firefox? You should have passed the event object in the function for it to work.

onKeyDown="javascript:return submitonEnter(event);"
Ben, why do you want to submit the form on press of Enter in the textarea? How can the user add new lines in the textarea then? If you dont want new lines, just use text field. Using text field, you dont need the code for auto submission as by default, forms with single text field will automatically be submitted on press of Enter in the field.

Bob Leny
08-02-2006, 05:43 AM
Alright! Thanks this works!



<html>
<body>

<script language="JavaScript">
function submitonEnter(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if(charCode == "13"){
document.form1.submit();
}
}
</script>

<form name="form1" action="index.php?mid=72me211v4604" method="POST">
<textarea name="msg" onKeyDown="javascript:return submitonEnter(event);"></textarea>
<br>
<input type="submit" value="Sänd/uppdatera">
</form>

</body>
</html>

boeing747fp
08-02-2006, 07:34 AM
o crap i forgot that "event" :P but yea it was in there when i first pasted it :P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum