0

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

Skeets
  • 3,794
  • 37
  • 60
  • Check [this](https://stackoverflow.com/a/52421085/14629458) to count emojis in your `textarea`. And since every emoji occupies 2 digits, the number of chars selected should be `selectionEnd - selectionStart - countedNumberOfEmojies` . – Yasser CHENIK May 05 '22 at 00:22

0 Answers0