Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Uppercasing keyboard input in mozilla

0 views
Skip to first unread message

alex

unread,
Jan 9, 2003, 5:56:17 AM1/9/03
to

I need to uppercase whatever the user types in TEXTAREA
box, etc. The following code works fine in IE. I'd like to
port it to Mozilla and Netscape 7. I tried using
event.which, but that did not work.

Thank you for helping,
alex

<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html">
<META NAME="GENERATOR" Content="Microsoft FrontPage 4.0">
<TITLE></TITLE>
<script LANGUAGE=javascript>

function Translate(e)
{
var key; // key pressed

if(!e)
var e = window.event;

key = e.keyCode;

if(key >= 97 && key <= 122) // lowercase letters
{
e.keyCode = key - 32; // uppercase
}
}

</script>
</HEAD>
<BODY>
<center>
<br>

<form method=post id=idfNotepad>
<TEXTAREA onkeypress=Translate() id=idfTextarea rows=18
cols=70></TEXTAREA>
</form>
</center>

</BODY>
</HTML>

Steve Fulton

unread,
Jan 9, 2003, 9:11:26 AM1/9/03
to

alex wrote:
> I need to uppercase whatever the user types in TEXTAREA
> box, etc. The following code works fine in IE. I'd like to
> port it to Mozilla and Netscape 7. I tried using
> event.which, but that did not work.

Gecko DOM Event Reference
http://www.mozilla.org/docs/dom/domref/dom_event_ref.html

Document Object Model (DOM) Level 2 Events Specification
http://www.w3.org/TR/DOM-Level-2-Events/

--
Steve

It often shows an excellent command of language to say nothing. -Karol Newlin


alex

unread,
Jan 9, 2003, 4:15:05 PM1/9/03
to

Steve,

Thank you very much for offering your help. I did look
into specs even before posting this message.

Unfortunately, I was not able to figure out the solution.

http://www.w3.org/TR/DOM-Level-2-Events/ tells me
that "The DOM Level 2 Event specification does not provide
a key event module."

http://www.mozilla.org/docs/dom/domref/dom_event_ref.html
lists keyCode as a valid property, although "Not part of
specification", but Mozilla does not recognize it.

Since you already took time to help me, could you please
be more specific, how to solve my problem.

Thanks again,
alex

>.
>

Etan Bukiet

unread,
Jan 9, 2003, 7:27:37 PM1/9/03
to
have you trieed the ".charCode" property of the event object

check the docs:
http://www.mozilla.org/docs/dom/domref/dom_event_ref6.html#1003390

etan


alex

unread,
Jan 10, 2003, 4:20:02 AM1/10/03
to
Etan,

Thank you very much for offering your help. I did

try .charCode property. It appears to be read only
property, the same as .which property.
When I try to assign a new key value, Mozilla's JavaScript
console reports an error: setting a property that has only
a getter.

Thanks again,
alex

>.
>

Steve Fulton

unread,
Jan 10, 2003, 12:01:36 PM1/10/03
to
alex wrote:
> Etan,
>
> Thank you very much for offering your help. I did
> try .charCode property. It appears to be read only
> property, the same as .which property.
> When I try to assign a new key value, Mozilla's JavaScript
> console reports an error: setting a property that has only
> a getter.

If Mozilla won't let you change the key code inside the handler, I guess you
have to take over the whole event.

function Translate(e) {
var key = e.charCode;
if (key >= 32) {
e.preventDefault();
if (key >= 97 && key <= 122) key -= 32;
e.target.value = e.target.value + String.fromCharCode(key);
}
}

[ Cross-browser version left as an exercise. ;-) ]

--
Steve

Nearly all men can stand adversity, but if you want to test a man's character,
give him power. -Abraham Lincoln


alex

unread,
Jan 10, 2003, 4:39:22 PM1/10/03
to
Steve,

e.target.value = e.target.value + String.fromCharCode(key);

is not what I need. It will append to what is accumulated
in TEXTAREA instead of inserting the char at cursor
position.

What I need is to get an event, find what key was pressed
and redefine the event as if a different key was pressed.

Something similar to what is easily done in IE:
e.keyCode = newkey;

Uppercasing TEXTAREA input was just a simple convenient
example to illustrate the problem, but my goal was not to
manipulate the contents of TEXTAREA directly, because the
target may not be the TEXTAREA.

Thank you again,
alex

>.
>

Paul Gorodyansky

unread,
Mar 20, 2003, 5:30:13 PM3/20/03
to
alex wrote:
>
> Steve,
>
> e.target.value = e.target.value + String.fromCharCode(key);
>
> is not what I need. It will append to what is accumulated
> in TEXTAREA instead of inserting the char at cursor
> position.
>
> What I need is to get an event, find what key was pressed
> and redefine the event as if a different key was pressed.
>
> Something similar to what is easily done in IE:
> e.keyCode = newkey;
>

Alex, did you find the solution? I've just met the same problem
and found your post via Google Groups searching for the error


"setting a property that has only a getter"


--
Regards,
Paul Gorodyansky
"Cyrillic (Russian): instructions for Windows and Internet":
http://ourworld.compuserve.com/homepages/PaulGor/

0 new messages