'Get selection offset of string in textarea containing multibyte characters

It seems that the selectionStart and selectionEnd properties of a textarea element in Javascript are not multi-byte safe, returning the wrong values for the selection offset when multibyte characters are present:

https://jsfiddle.net/skeets23/wdnrj89k/16/

Is there any way to work around this and get the correct selection offsets?

My usage case is: I want to allow the user to save a section of "highlighted" text in a given paragraph. So they can select text and press "save", and it sends the "start" and "end" value of their selection to the back end (which is already multi-byte compatible (PHP, so mb_* functions))

I tried disabling multi-byte compatibility on the back end in hopes that at least the front end and the back end would treat multi-bite strings in the same way, but that wasn't the case... it gave me garbled text :P



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source