Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submitting from a textfield

Status
Not open for further replies.

GezH

Programmer
Aug 7, 2002
68
Hi all.

I have a form with a number of different standard form features, including one textfield.

Often, users fill in the textfield and hit return to submit the page, rather than bothering to press the 'submit' button.

How can I capture this please? I want to know when the use has hit return, and do some stuff in a small javascript method.

Thanks for any help!
 

Do you want to know when they have hit return in a textfield, or do you want to know if the form is submitting? These are two very different questions.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Erm...

Well, what I want is... when they hit return in the textfield, I still want the form to submit. But, I need to perform some javascript first, so that the java which intercepts the request knows gets the correct info. So I guess I need to know when they have hit return in a textfield, yes!
 
This works for me:

Code:
<html>
<head>

	<script type="text/javascript">
	<!--

		function keyPressed(evt) {
			var keyCode = evt ? evt.keyCode : event.keyCode;
			if (keyCode == 13) alert('Enter was pressed in the text field!');
		}

		onload = function() {
			document.getElementById('myTextField').onkeypress = keyPressed;
		}

	//-->
	</script>
</head>
<body>
	<form>
		<input type="text" id="myTextField" />
	</form>
</body>
</html>

I'm still not clear about why you cannot just use the onsubmit event of the form object, if you are looking to trap submission of the form... but who am I to argue with unknown specs!

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
...if I use onsubmit, how do I know whether the use hit the button or pressed return in the textfield?

Thanks for the code, I'll give it a try, though I'm already using an id parameter...
 

Just change the id in the code for your id, then.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
GezH,
You can as well set it up like this.
[tt]
<input type="text" onkeypress="return evthandler(event) />
[/tt]
With the handler like this.
[tt]
function evthandler(e) {
var kcode=e.which?e.which:e.keyCode;
return (kcode==13)?false:true;
}
[/tt]
regards - tsuji
 
Correction (typo):
[tt] <input type="text" onkeypress="return evthandler(event)[red]"[/red] />
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top