471,352 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,352 software developers and data experts.

trouble with charCode

I'm running the following function in Firefox. I've thrown the alerts
in to help figure out where the error is. The code terminates at the
line
var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which
: evt.keyCode);
I get no error in the javascript console, the code just stops cold (I
don't get the second alert message).
Do you have any idea why?

function parseDateField(evt) {
evt = (evt) ? evt : ((window.event) ? event : null);
if (evt) {
var elem = (evt.target) ? evt.target : evt.srcElement;
if (elem) {
alert ("break 1");
var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ?
evt.which : evt.keyCode);
alert ("charCode = " + charCode);
return true;
} else {
return false;
}
}
}

Its being called in the body of the page here
<INPUT TYPE="text" NAME="txtDate" id="txtDate" SIZE="9" MAXLENGTH="9"
onkeypress="parseDateField(this)">

May 4 '06 #1
1 3168

br**********@wpafb.af.mil wrote:
I'm running the following function in Firefox. I've thrown the alerts
in to help figure out where the error is. The code terminates at the
line
var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which
: evt.keyCode);
No it doesn't. It never gets to that line.
I get no error in the javascript console, the code just stops cold (I
don't get the second alert message).
Do you have any idea why?
You call this as <INPUT ... onkeypress="parseDateField(this)"> so the
- this - keyword in the onkeypress handler of the INPUT element is a
reference to the INPUT element. It is this reference that you pass to
your - parseDateField - function as its - evt - argument.
function parseDateField(evt) {
evt = (evt) ? evt : ((window.event) ? event : null);
The - evt - parameter is always a reference to the INPUT element, so it
is true.
if (evt) {
The - evt - parameter is still true here.
var elem = (evt.target) ? evt.target : evt.srcElement;
INPUT elements do not have - target - or - srcElement - properties so
the value assigned to - elem - is undefined.
if (elem) {
Undefined type-coverts to boolean false so this branch of the if-else
is never entered, and the - else - branch is taken.
alert ("break 1");
var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ?
evt.which : evt.keyCode);
alert ("charCode = " + charCode);
return true;
} else {
return false;
The - else - branch just returns false, with no errors generated (or
expected).
}
}
}

Its being called in the body of the page here
<INPUT TYPE="text" NAME="txtDate" id="txtDate" SIZE="9" MAXLENGTH="9"
onkeypress="parseDateField(this)">


Replace the onkeypress handler with:-

onkeypress="parseDateField(event, this);"

- and add a second parameter to the function to recieve the INPUT
element passed with - this -(saves messing about with
target/srcElement). Or keep passing the - this - and treat the value
that arrives as the reference to the element you want to act upon not
the event object.

Richard.

May 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.