Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Allow only caps at key press in firefox
Old 06-09-2006, 08:22 AM Allow only caps at key press in firefox
Junior Talker

Posts: 2
Trades: 0
in IE i m getting ascii/unicode using event.keyCode..

for "a" i m getting 97. and when i assign as below..

i m getting it in variable i...

event.keyCode = i - 32. then it will be converted in "A" and will be displayed in textbox.

but it is not working with firefox.... if anybody have any idea then please tell me.. i need it urgently.......


i tried with followed event methods...

event.keyCode, event.charCode, event.which........
gautamtrivedi is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-09-2006, 02:32 PM Re: Allow only caps at key press in firefox
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
window.event is an IE-specific thing. W3C-compliant browsers (read: mostly everything but IE ;P) should pass event data to the function, just make your function accept a parameter ('e' for example). Here is an example that probably suits your needs exactly, right from QuirksMode:

Code:
function doSomething(e)
{
    if (!e) var e = window.event
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    
    // Use code as the keycode for all browsers
}
You can find this code snippet and more useful info at QuirksMode: http://www.quirksmode.org/js/introevents.html
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 06-10-2006, 03:04 AM Re: Allow only caps at key press in firefox
Junior Talker

Posts: 2
Trades: 0
following is the code i m running... but as per your answer its not working...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function doSomething(e)
{
if (!e) var e = window.event

if(e.keyCode){

code = e.keyCode;
if(code > 64 && code < 91)
e.keyCode = code - 32;

}else if(e.which){

code = e.which;
if(code > 96 && code < 122)
e.which = code - 32;

}

}
</script>
</head>

<body>
<input type="text" id="txt" name="txt" onkeyup="doSomething(event)"/>
</body>
</html>
gautamtrivedi is offline
Reply With Quote
View Public Profile
 
Old 06-10-2006, 02:22 PM Re: Allow only caps at key press in firefox
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
To W3C browsers, it is only information. See for an example of how to do what you want cross browser.

But I think you are going about it in a more complicated way then is necessary. You can just toUpperCase it on onkeyup and you can get almost the same effect.


Code:
<input type="text" id="txt" name="txt" onkeyup="this.value=this.value.toUpperCase()" />
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 06-10-2006, 11:43 PM Re: Allow only caps at key press in firefox
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
Chroder's method is probably the better way than trying to override an event, especially when those events should be read-only it's just IE seems to think they can be overriden.

Although you could use onkeyup or onblur, I would prefer changing it when focus is lost (onblur) than calling it on each keyup.

Shouldn't really matter, because your script that handles the form should convert it to uppercase anyways instead of relying on the input to be uppercase.

If you want to capture characters a more compatible way then:

Code:
var ch = (event.keyCode) ? event.keyCode : event.charCode;
event.which is a real old method which was used in Netscape and does work in Firefox, but I find charCode more compatible for all other browsers, keyCode is IE specific but a few browsers may support it too.

Events should be read-only, so you can't override them, just seems Firefox has kept it that way.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.

Last edited by mastercomputers; 06-10-2006 at 11:47 PM..
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Reply     « Reply to Allow only caps at key press in firefox
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30202 seconds with 12 queries