Recently i had to face this problem and this is how i nailed down...
- Get the string index at the cursor position in the textarea by using selectionStart
- slice the string from index 0 to the cursor position
- Insert it into a span (since span has multiple border boxes)
- Get the dimensions of the border box using element.getClientRects() relative to the view port. (here is the MDN Reference)
- Calculate the top and left and feed it to the dropdown
This works in all latest browsers. haven't tested at old ones
Here is Working bin