[thelist] JavaScript "onKeyPress" problem

MRC webmaster at equilon-mrc.com
Thu Feb 21 19:14:00 CST 2002


Syed,

> However, one thing is confusing. What is the philosophy behind this
> problem and it's solution? Why didn't the script worked with "!=" and
> "return false"? What was wrong?

    It appears that your message was delayed by a few hours (by the outage
afflicting the Evolt list, I assume), so you probably already have your
answer. But, in case you missed it, Phil Parker (BT Bigpant) gave a good
explanation of why either [== OR] or [!= AND] would give you the result that
you wanted.
    The reason your approach didn't work, however, was that by using [!=
OR] -- essentially, if x doesn't equal a or if x doesn't equal b -- you will
typically _always_ get a true condition (assuming that a and b are not
equal). When your if statement evaluated to true (which it typically always
would), your script returned false to the event handler, thus preventing any
key entry into the textarea.
    As an aside, you could have simplified your script even further by
writing it like this:

function checkTheKey()
{
  return (event.keyCode==48 || event.keyCode==49 || event.keyCode==50) ;
}

James Aylard




More information about the thelist mailing list