Forum Moderators: open

Message Too Old, No Replies

Firefox event.keyCode problem (help.)

Firefox javacsript issue

         

Richi

11:58 am on Sep 22, 2005 (gmt 0)



Help….

I’ve got a textarea where people type in a description. However for certain reasons we need to stop them typing!$*^ .

I have a solution this which works fine in IE:

function keypress()
{
if((event.keyCode==33)¦¦(event.keyCode==36)¦¦(event.keyCode==42)¦¦(event.keyCode==94))
{
//bad key pressed
event.returnValue = false;
}
}

This function is in a javascript file and called from the textarea like this:

<textarea rows="13" maxLength="860" cols="30" name="remarks" id="remarks" title="description"
onchange="keypress();" onKeypress="keypress();" onKeyDown="keypress();" onKeyUp="keypress();"
value =""></textarea>

However, it doesn’t work in Firefox. I changed event.keyCode to event.which . However, this only works if the javascript is embedded in the html and not in my .js file. However, one of the requirements for my project is that it is W3c compliant and therefore the javascript should be in separate files and not embedded in the html.

Does anyone know how I can successfully get the event object in a javascript file using firefox so I can user event.which?

Cheers…Rich

dcrombie

12:18 pm on Sep 22, 2005 (gmt 0)



The global 'event' variable is AFAIK only implemented in Explorer.
This should sort you out:
[developer.apple.com...]

;)

Bernard Marx

2:48 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<textarea onkeypress="keypress(event)"></textarea>

function keypress(e)
{
if({ 33:1, 36:1, 42:1, 94:1 }[e.which¦¦e.keyCode])
e.preventDefault? e.preventDefault() : e.returnValue = false;
}

Replace the corrupted ¦¦ with unbroken pipes

AWildman

3:31 pm on Sep 22, 2005 (gmt 0)

10+ Year Member



You must pass the event object into your function in order to reference it (in netscape & firefox).

See the heading "Accessing the event" on this page:

[quirksmode.org...]

let us know if you need more help...