Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

BBS: Re: How to use onkeypress to get a key in a textbox - August 19, 1998 at 06:29:02

You are here: irt.org | BBS | Re: How to use onkeypress to get a key in a textbox [This BBS is closed]

Posted by Jason Nugent on August 19, 1998 at 06:29:02:

In Reply to: How to use onkeypress to get a key in a textbox posted by john on August 16, 1998 at 21:59:54:

: I built a form to retrieve user name and password, after user enter both items, and press <Enter> key, I want to capture it using Javascript, and send a form.submit () command to call next page. How can I get the event object?

: Obviously, <input type=text onkeyPress="press (e)"> will not work.

It's different for both Netscape and IE. With Netscape, you must first register your window object to capture the keypress event:

<SCRIPT Language=:"JavaScript1.2">
window.caputureEvents (Event.KEYPRESS);

... and then define a function to handle the event...

function keyHandler (e){
function information...
}

</SCRIPT>

In Netscape, you have the option of using which (), which returns the ASCII value of the key that was pressed. Just figure out which one is the enter key and check for that.

if (e.which () == "enter ascii value")
return true;
else return false;

Internet Explorer uses "event bubbling", in which an event slowly bubbles up through the elements on a page, giving you the option of capturing it at higher and higher levels (a single function to handle all mouseOvers, for example). You might get the ENTER key this way:

onKeyPress="check";

function check (){
e = event.srcElement;
if (e.keyCode () == "Enter ASCII value")
return true;
else return false;
}

Notice that there are two ways to do it, and they are both different for each browser. They also only work in version 4 browsers so you have think of something else for older ones. Its a pain, but this is what you have to do if you want to code cross-browser, unfortunately.
I hope this helps,

Jason
Follow-ups:

You are here: irt.org | BBS | Re: How to use onkeypress to get a key in a textbox [This BBS is closed]

©2018 Martin Webb