Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I can't get onKeyPress or onKeyDown to work in Firefox 1

Status
Not open for further replies.

JA3395

Programmer
May 22, 2007
88
IE
I'm having real problems with Firefox and the onKeyPress / onKeyDown event handlers.

Just for a change, both work fine in IE!

Code:
<script language="Javascript">
function capLock(divID,e){
  kc = e.keyCode;
  if (kc == 0) kc = e.charCode;
  
  if(kc >= 65 && kc <= 90) {
    document.getElementById(divID).style.visibility = 'visible';
  } else {
    document.getElementById(divID).style.visibility = 'hidden';
  }
}
function HideCapLock(divID) {
  document.getElementById(divID).style.visibility = 'hidden';
}
</script>
Code:
<span><input name="Password" id="password" type="password" autocomplete="off" length=20 onClick="Javascript:this.value=''" onChange="Javascript:verifyPassword(this.id);" onKeyDown="Javascript:capLock('divCapsLock1',window.event);" onBlur="Javascript:HideCapLock('divCapsLock1');" />
<div id="divCapsLock1" style="visibility:hidden;margin-top:-80px"><img style="margin-left:140px;" src="../images/buttons/caps_lock_right.png" /></div></span>

<span><input name="CPassword" id="cpwd" type="password" autocomplete="off" length=20 onClick="Javascript:this.value=''" onChange="Javascript:verifyPassword(this.id);" onKeyPress="Javascript:capLock('divCapsLock2',window.event);" onBlur="Javascript:HideCapLock('divCapsLock2');" />
<div id="divCapsLock2" style="visibility:hidden;margin-top:-80px"><img style="margin-left:140px;" src="../images/buttons/caps_lock_right.png" /></div></span>

The browser seems to totally ignore both events, it doesn't go near the function.

Any suggestions?

Originally I posted in "Browser Issues" as this is a FireFox problem, but I was redirected here, sort of unhelpful, but there you go!
 
You want something like..

if(window.event)
keyPressed = window.event.keyCode; // IE
else
keyPressed = e.which; // Firefox

* <input type="text" name="txt1" onKeyPress="SearchName(this,selList,event)" value="<Type To Search>" onClick="javascript:this.value='';" style="width:200px;">



Ordinary Programmer
 
Thanks so much, I knew it would be quicker to ask in Tek-Tips!

Some things are just not well documented...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top