trouble with charCode

B

brian.newman

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)">
 
R

Richard Cornford

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top