I'm building a custom input field in javascript. (i.e. not using content editable, input, textarea or iframes).
I need it to support the basic options in a textarea field e.g.:
- Mouse click for cursor position
- Selection
- Copy/paste
...
At this point i'm listening for keypress on an underlying input field.
When a key is pressed:
1. the value of the key String.fromCharCode(event.keyCode)
is passed to a tmp span.
2. The width of the span is measured, and stored in an array.
3. the caret position is calculated using the array of sizes
This works more or less in chrome, but when using internet explorer 9, the caret seems to get out of position.
It seems the width of the container with a single container, does not match the width the that character has when it's inserted into a string.
I'm guessing this has something to do with how the size of the letter is calculated in step 2. But i can't quite figure out how to go about this problem.
Does anyone have experience with the issue or point me in the right direction with some litterature, blogs apis, anything? - that would be great!
edit: Here is a link to what i've got so far;
NB: works in chrome, and has the mentioned defect in ie: 9 and 10 it's broken in firefox :)