Getting and Setting key character in key event

J

Jeff

I have had this function work perfectly in IE and am trying to get it
to work in Firefox. I have seen plenty of questions and answers on the
web for how to get and check the key pressed but nothing about setting
or changing it. I only want uppercase characters in this case. If the
key is a lowercase, it needs to be changed to an uppercase character.
IE lets you use keyCode to change the character code. How do I do the
equivalent in Firefox?

<textarea cols="40" rows="3" name="Rule" id="Rule"
onkeypress="checkUppercase(event);"></textarea>

function checkUppercase(e) {
// Get ASCII value of key that user pressed
if (!e) e = window.event;
var key = e.keyCode ? e.keyCode : e.which;

// Was key that was pressed a lowercase letter from a-z or backspace?
if ( key < 97 || key > 122 || key == 8 )
return; // if so, do nothing
else // make it uppercase
if (window.event)
e.keyCode = key - 32; // IE
else
// None of the following work for Firefox
e.which = key - 32;
e.charCode = key - 32;
}

Thanks,
Jeff
 
T

Thomas 'PointedEars' Lahn

Good said:
Is this really all you need? ie: just the text in uppercase, and
nothing about 'character codes'? if so, you're doing wayyyy too much
work (untested code below)

<textarea cols="40" rows="3" name="Rule" id="Rule"
onchange="upperMe(this);"></textarea>

then:

function upperMe(thetextarea) {
var field = document.getElementById(thetextarea);
var thein = field.value;
var theout = thein.toUpperCase();
field.value = theout;
}

Won't work. `this' in the `onchange' event handler attribute is an object
reference; D::gEBI() expects a string (DOMString) ID as argument.

I would also omit some assignments and do a proper feature test instead.
However, all of this is unnecessary here.
or even simpler, drop the function altogether:

<textarea cols="40" rows="3" name="Rule" id="Rule"
onchange="document.getElementById('Rule').value.toUpperCase();">
</textarea>

Won't work either. String::toUpperCase() does not modify the string value,
it just returns the a value (strings are immutable). Since the returned
value is not assigned to anything, it does not affect anything.

The following works everywhere (there is not even a feature test required):

<textarea cols="40" rows="3" name="Rule"
onchange="this.value = this.value.toUpperCase();"
</textarea>
[Top post]

Please trim your quotes.

http://www.jibbering.com/faq/faq_notes/clj_posts.html

A sender address that complies with RFC1036 and RFC2822 will also prove to
be a wise decision in the long run. Especially since letsgo.com already has
an owner.


PointedEars
 
R

Richard Cornford

Thomas said:
Good Man wrote:

Won't work either. String::toUpperCase() does not modify the
string value, it just returns the a value (strings are immutable).
Since the returned value is not assigned to anything, it does not
affect anything.

The following works everywhere (there is not even a feature test
required):

<textarea cols="40" rows="3" name="Rule"
onchange="this.value = this.value.toUpperCase();"
<snip>

As a related comment; CSS text-transform (- text-transform:uppercase; -)
seems to work reasonably reliably with text input and textarea fields in
modern/current browsers. It has a presentational effect, displaying all
the entered text in uppercase regardless of the case of the characters
being typed. The underlying value of the element is not altered by this
presentational effect, but so long as the contents of the field are
themselves transformed (onchange or onblur) its use can give the user a
more informative experience as they type (that is, they can see that
what they are typing is going to be handled in uppercase), and the
actual transformation becomes unobservable to the user.

Richard.
 
J

Jeff

Thanks for the replies. I want to do the same thing in Firefox that
happens in IE, and that is changing the character as it is typed so
that means not using the onchange event. Is there no way to do this in
Firefox?

In the meantime, thanks to a combination of replies, this will work,
although I still would like to do it character by character:

<textarea cols="40" rows="3" name="Rule" style="text-
transform:uppercase;"
onchange="this.value = this.value.toUpperCase();"></textarea>

Thanks,
Jeff
 
J

Jeff

doing it character by character will be infuriating to your users. try it
yourself. What happens when you try to backspace/enter a period/return
key, etc... It really interferes with the user experience IMHO.

It has been working that way for over a year with no complaints from
the users. BTW, if anything other than a lowercase character is typed,
the function exits. The users are on an Intranet and as of right now
are only using IE but now we are making sure Firefox users will be
able to use the site. So I take it that there is no equivalent way in
Firefox to change the character typed like IE can do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top