Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript caret position

I'm developing a mobile banking application and I'm interested in formatting the currency amount inputs in real time.

I've already tested autoNumeric plugin and jQuery Format Currency Plugin but both have cursor position issues on Android 2.* browsers.

Does anyone have a JavaScript solution compatible with this browsers?

like image 679
Diogo Cardoso Avatar asked Aug 10 '12 11:08

Diogo Cardoso


People also ask

What is caret position in JavaScript?

Javascript - Caret Position The mouse moves the cursor. Text fields use a caret to indicate where text is inserted. Left clicking the mouse typically sets the caret location.

What is the caret position?

In computing, caret navigation (or caret browsing) is a kind of keyboard navigation where a caret (also known as a 'text cursor', 'text insertion cursor', or 'text selection cursor') is used to navigate within a text document.

How do you find the input caret position?

If you ever had a specific case where you had to retrieve the position of a caret (your cursor's position) inside an HTML input field, you can do so using the selectionStart property of an input's value.

How do you get the caret position in an editable div?

To get contentEditable caret position with JavaScript, we change the selection to start from the beginning and then get the length of the full selected string. const cursorPosition = () => { const sel = document. getSelection(); sel.


1 Answers

I don't know about autoNumeric, but http://code.google.com/p/jquery-formatcurrency/ looks pretty good. The jsFiddle example you posted doesn't place the caret correctly on my desktop browser, but this does, and on my (Jellybean) Android phone as well. (On Gingerbread, the blinking cursor bar may jump to the end of the line, but you will still be editing where it last was.) Try their demo: http://www.bendewey.com/code/formatcurrency/demo/format_as_you_type.html

like image 116
Sparky Avatar answered Sep 23 '22 14:09

Sparky