1. Firstly, why not do it onkeydown, because a character is generally drawn after onkeydown, not onkeyup (at least for IE).
2. There may be a simple answer of capturing the "onkeydown" keycode on the event and overriding it with the desired value. I am not sure if this would work, but is an idea.
// IE ONLY FUNCTION
document.onkeydown=function()
{
var nKey=window.event.keyCode;
var nNewKey=(String.fromCharCode(nKey).toUpperCase()).charCodeAt(0);
window.event.keyCode=nNewKey;
}
3. A more messy method could be to run a "captialise function" on the whole inputbox/textarea contents on each key up (but this may lose the cursor).
4. The most sophisticated method would be based on the range and selection objects.
There are two different range objects depending on whether you use IE or Mozilla.
When a keydown event occurs do something like the following (IE only, not tested):-
// IE ONLY FUNCTION
document.onkeydown=function()
{
var nKey=window.event.keyCode;
var sNewKey=String.fromCharCode(nKey).toUpperCase();
var oSel=window.selection;
var oRange=Sel.createRange();
oRange.collapse(true); // Move to start of range
oRange.text=sNewKey;
oRange.collapse(false); // Move to end of range
oRange.select();
window.event.returnValue=false; // Stop original key press
}
1. Sentence case presumably means that you will need to look at the "context" of any key press. I.e. you cannot simply tell from the keyCode whether it should be upper or lower case, but will need to look back at what has been typed so far.
This could be a complex thing to do at the time of an onkeyup event. You may wish to apply it later.
2. If you are going to do it, then my suggestion number 3. in the first post will be perhaps the way to do it. I.e. apply a SentenceCase function to the whole contents of the text box (or at least the contents up to the cursor) after each Key press
3. A Sentence case function may be a non trivial function.
I.e. the computer will need to determine whether the character should be capital or not depending on whether it is the start of a sentence.
This could be non-trivial if the range of subjects being typed could include names (which need a capital first letter), web addresses (which use lots of dots), etc.
You may wish to search on Google for examples of such functions.
Sorry about case and caps. This is what happens if one has limited knowledge. Thanks for the script. Got more than what I expected. No simultaneous dots. So one cannot type etc.....
Just one more thing. How do I use the script for 2 or more text boxes on the same page?
No prob! It is just that in German, not only names but many words (eg all nouns) have to start with upper case; so nobody in Germay would need such a script and I only learned about sentence case when asking a person here with a similar request.
Regards - Pit
The perfect website, which isn't one: http://www.pit-r.de (completely re-designed)
Bad news for many "important" members here: I am still alive.
What modifications need to be made to the above script so that it can accept 3 letters in caps. Eg. USA. If one types 4 or more letters in caps consecutively then it should make it in lower case. Eg. COUNTRY should become country and not COUntry.
Bookmarks