Determine cursor position on textfield

C

Cah Sableng

Hi all.
I am new member of this group.

I have a problem with currency formatter function I made. On onkeyup's
method of a textfield I add function below:

<snip>
function formatCurrency(elm) {
var str=elm.value;
var re = /(-?\d+)(\d{3})(,\d*)?/
var re1 = /\./g

str = str.replace(re1,'')
arr = str.split(',')
str = arr[0]

while (re.test(str))
str = str.replace(re, '$1.$2$3')

str += (arr.length > 1) ? (',' + arr[1]) : '';

elm.value = str
}
</snip>

It works as I expected, except that after the function finished its
execution the cursor moves to the end of the textfield. So if I want
to insert two or more characters, the rest of it goes to the end of
textfield.
How I determine the position of the cursor in the textfield before
executing the regex?

Sorry for the bad english I wrote.
TIA.
 
E

Elegie

Cah Sableng wrote:

Hello,

How I determine the position of the cursor in the textfield before
executing the regex?

With 'el' being your form control, try using :

function getCaretPos(el) {
var rng, ii=-1;
if(typeof el.selectionStart=="number") {
ii=el.selectionStart;
} else if (document.selection && el.createTextRange){
rng=document.selection.createRange();
rng.collapse(true);
rng.moveStart("character", -el.value.length);
ii=rng.text.length;
}
return ii;
}

You'll find plenty of examples in the newsgroup archives, located at
<URL:http://groups.google.fr/group/comp.lang.javascript/topics>


HTH,
Elegie.
 
C

Cah Sableng

Cah Sableng wrote:

Hello,



With 'el' being your form control, try using :

function getCaretPos(el) {
var rng, ii=-1;
if(typeof el.selectionStart=="number") {
ii=el.selectionStart;
} else if (document.selection && el.createTextRange){
rng=document.selection.createRange();
rng.collapse(true);
rng.moveStart("character", -el.value.length);
ii=rng.text.length;
}
return ii;

}

You'll find plenty of examples in the newsgroup archives, located at
<URL:http://groups.google.fr/group/comp.lang.javascript/topics>

HTH,
Elegie.

Thank you very much. That's what I'm looking for.
 
L

Lee

Cah Sableng said:
Hi all.
I am new member of this group.

I have a problem with currency formatter function I made. On onkeyup's
method of a textfield I add function below:

It works as I expected, except that after the function finished its
execution the cursor moves to the end of the textfield. So if I want
to insert two or more characters, the rest of it goes to the end of
textfield.

That's a good example of why you should wait until the onchange
event handler to reformat the field. Reformatting after each
keystroke is inefficient and is annoying to the user.


--
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top