473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 3239

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Thomas Christensen | last post by:
I'm trying to figure out what key the user pressed using a Danish keyboard layout. charCodeAt returns the correct number, but event.keyCode returns a wrong number, when using one of the keys that...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
6
by: jh3an | last post by:
How are you ? :) This code works on Internet Explorer: function(event){ event.keyCode = 'a'; } So, No matter what you typed in the textbox or textarea, you would get 'a'.
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.