View Full Version : Trapping Enter key
JHobbs
12-23-2002, 06:26 PM
Hi All,
I want to trap the enter key when the user is typing into an text edit control. Here is the code that I've tried:
function checkCR() {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) "verify();" }
document.onkeypress = checkCR;
Any assistance will be greatly appreciated.
Thanks
ez4me2c3d
12-23-2002, 06:45 PM
function isEnter(evnt) {
evnt = (evnt) ? evnt : ((event) ? event : null);
if (evnt) {
var charCode = (evnt.charCode || evnt.charCode == 0) ? evnt.charCode : ((evnt.keyCode) ? evnt.keyCode : evnt.which);
if (charCode == 13) {
alert("you pressed enter");
}
}
}
and just place the onKeyPress="isEnter()" into your textarea tag
JHobbs
12-23-2002, 08:25 PM
thanks for your reply Anthony; however, I still get the same results (a beep) when I hit "enter".
You suggested in your code example that I use a textarea tag and that's probably my problem. I'm using a regular text box and it looks like this control won't allow carriage returns.
Is there anyway to use this type of control or do I have to use a textarea?
Thanks
ez4me2c3d
12-23-2002, 08:30 PM
when you hit return in a text area the form will try to submit.
PS the beep comes from the line:
alert("you pressed enter");
replace that with something else to happen when you catch the user pressing enter.
May i ask what this is for? mayeb there is another way to accomplish your task.
JHobbs
12-23-2002, 08:46 PM
I replaced the alert with some other code and still get the "beep".
The purpose of this code is when the user presses "Enter" at any time when they are adding data to the text edit controls, the form should be submitted (or rather I want the javascript that checks the validity of the data executed which if successful will submit the form)
Thank you very much for your help, Anthony.
JHobbs
12-23-2002, 09:00 PM
Sorry Anthony. My mistake. I had
input type=button
instead of:
input type="submit"
thanks again
ez4me2c3d
12-24-2002, 12:07 AM
do you have a working example of the page that we can see?
also there are others was to validate a form and you don't have to trap the enter ket pressing.
JHobbs
12-24-2002, 12:16 AM
Thanks for your help Anthony. The Submit typo fixed the problem. It's working now. I wouldn't have found the problem very easily without your direction.
Thanks again
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.