Restoring Caret Position on text field correction

B

Barry

Retoring Caret Position after text field correction

--------------------------------------------------------------------------------

Hi, my code has the following form -

function ConvertOld(myfield)
{
if (myfield.inchange)
return;
myfield.inchange=true;

myfield.value = myfield.value.replace(/A\//g,"Á");
myfield.value = myfield.value.replace(/a\//g,"á");
myfield.value = myfield.value.replace(/E\//g,"É");
myfield.value = myfield.value.replace(/e\//g,"é");
myfield.value = myfield.value.replace(/I\//g,"Í");
myfield.value = myfield.value.replace(/i\//g,"í");
myfield.value = myfield.value.replace(/O\//g,"Ó");
myfield.value = myfield.value.replace(/o\//g,"ó");
myfield.value = myfield.value.replace(/U\//g,"Ú");
myfield.value = myfield.value.replace(/u\//g,"ú");
myfield.value = myfield.value.replace(/e\//g,"é");

myfield.inchange=false;
}

<form action="">
<input type="text" size="20" maxlength="50" name="myinput"
onKeyUp="ConvertOld(this)"/>

The problem is that the caret is forced to the end, when ever a key
such as the back button is pressed, and the user cant correct a
mistake within the textbox by moving back to it.

One change I made was -

var myStr = "";

function ConvertOld(myfield)
{
if(myfield.value == myStr)
return;

myStr = myfield.value;


But this isn't ideal. The user can now more the caret within the text
field, but once they change something, like deleting a letter the'd
like to change, the caret is forced to the end again.

For example -

User enters -

Intarnet

then realizes their mistake and moves cursor to position three in
order to delete the "a" and change it to an "e", doing so gives -

Intrnet

but the cursor is moved to the end, and the user has to move back to
position 3 in order to enter the "e" -

Any solutions?

Cheers,

Barry.
 
I

Ivo

Barry said:
Retoring Caret Position after text field correction

-------------------------------------------------------------------------- ------

Hi, my code has the following form -

function ConvertOld(myfield)
{
if (myfield.inchange)
return;
myfield.inchange=true;

myfield.value = myfield.value.replace(/A\//g,"Á");
myfield.value = myfield.value.replace(/a\//g,"á");
myfield.value = myfield.value.replace(/E\//g,"É");
myfield.value = myfield.value.replace(/e\//g,"é");
myfield.value = myfield.value.replace(/I\//g,"Í");
myfield.value = myfield.value.replace(/i\//g,"í");
myfield.value = myfield.value.replace(/O\//g,"Ó");
myfield.value = myfield.value.replace(/o\//g,"ó");
myfield.value = myfield.value.replace(/U\//g,"Ú");
myfield.value = myfield.value.replace(/u\//g,"ú");
myfield.value = myfield.value.replace(/e\//g,"é");

myfield.inchange=false;
}

<form action="">
<input type="text" size="20" maxlength="50" name="myinput"
onKeyUp="ConvertOld(this)"/>

The problem is that the caret is forced to the end, when ever a key
such as the back button is pressed, and the user cant correct a
mistake within the textbox by moving back to it.

In Win/IE only, you can move to and or select any text you want using the
TextRange object. But it needn't be as difficult as that. From your code it
is clear that we only need to detect the uppercase V. If it isn't, return
true immediately. Next, if the letter preceding this V isn't one of ours,
return true. The cursor will then behave as normally. Else, we have a match
and shall have to change the content of the box. This will leave the cursor
at the end and only a TextRange can solve that asaik.
Then there is the problem that somebody might actually mean to write
"U\/"...

If I knew a reliable crossbrowser way of reading the event.keyCode from
inside teh function, I 'd do that. This needs to all on one line:
<input onkeypress="
var k = event.keyCode ? event.keyCode :
event.charCode ? event.charCode : event.which;
k = String.fromCharCode(k);
if ( k != 'V' || !/[aeiouAEIOU]V/.test(this.value) )
return true;
else
Convert(this.value); "

<!-- If you change onkeyup to onkeypress the user won't even see the combo,
no matter how slow s/he types. That 's the only difference as far a I can
see. -->

HTH
Ivo
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top