File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes how do I access the onKeyPress value? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "how do I access the onKeyPress value?" Watch "how do I access the onKeyPress value?" New topic
Author

how do I access the onKeyPress value?

George Brown
Ranch Hand

Joined: Sep 26, 2000
Posts: 919
I have the following javascript code in the HEAD section...

And I have the following line in a form on the page:

The problem is that I don't know how to access the value of the keypress that triggered the event.
Can anyone help me out here, JavaScript is not my forte...
omar khan
Ranch Hand

Joined: Feb 01, 2001
Posts: 183
The return value of a JavaScript event handler is usually used to indicate the disposition on an event, namely to perform or nor a certain action.
What are you trying to do?
[ April 11, 2003: Message edited by: omar khan ]
Rajeshwari Natarajan
Ranch Hand

Joined: Mar 05, 2003
Posts: 67
If u want to know which key was pressed, then u can try this...
in the form

and in the script


regards<br />Rajeshwari. N
Sanjay Jadhav
Ranch Hand

Joined: Feb 21, 2002
Posts: 86
hi
As Rajeshwari siad it will work but not in netscape so may u need cross-browser javascript , as IE and Netscape handle the javascript event in different format.So u need the common javascript for both browser.
sanjay_jk_2000@yahoo.com


Sanjay Jadhav<br />Sun Certified Programmer For Java 2 Platform
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
look at this:
http://www10.brinkster.com/A1ien51/Scripts/KeyCode.htm
George Brown
Ranch Hand

Joined: Sep 26, 2000
Posts: 919
Thanks, all. That was very helpful - the information I was scrabbling for was the event.keyCode() method.
Is there an equivalent I could use for IE and NN? Or a resource I could look at to find examples of cross-platform js event handling?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
if you looked at my link it shows the IE and NN Stuff, but look at this code
<html>
<head>
<script>
function checkTime(X) {
Str=X.value;
StrLen=Str.length;
StrT=Str.slice(StrLen-1,StrLen);
var StrRE1 = /^\d/;
var StrRE2 = /^[0-2]/;
var StrRE3 = /^:/;

if(StrT.match(StrRE1) && StrLen>1 && StrLen<6) {
if(Str.length==2)Str+=":";
X.value=Str;
if(Str.length==5){X.blur();return false;}
else return true;
}
else if (StrT.match(StrRE2) && StrLen==1){
return true;
}
else if (StrT.match(StrRE3) && StrLen==3){
return true;
}

else {
Str=Str.slice(0,StrLen-1);
X.value=Str;
return true;
}
}
</script>
</head>
<body>
<form name="P">
<input type="text" name="B" onkeydown="checkTime(this)" onkeyup="checkTime(this)">
</form>
</body>
</html>
There is one problem that I have with it, and I am going to try to fix, You can not edit it after it is changed!
Eric
 
 
subject: how do I access the onKeyPress value?
 
Threads others viewed
disable other textfield as one textfield entered
Help with this code
Help with this code
validation
problem with date validation
developer file tools