5

I want to add vowels to chars in a textbox. I created inputs and a function which adds the value of the vowel into the textbox:

<textarea dir="rtl" id="text" runat="server" name="text" rows="5" cols="30""></textarea> <br />
<input type="button" style="height:28px;width:42px;" id="Dagesh" value="דגש" onclick="AddNikud(&#39;ּ&#39;)"/>

<script>
function AddNikud(nikud) {
   document.getElementById('text').value += nikud; 
   document.getElementById('text').focus();
}
</script>

The thing is - the function adds the vowel into the last char in the textbox. I want the vowel to be entered after where the keyboard cursor of the user is standing.

For example:

textbox: Hello wo(User is here)rld!

The vowel value should be from the right of the 'o'.

Thanks in advance!

emrhzc
  • 1,347
  • 11
  • 19
Kfir Sadeh
  • 75
  • 5

1 Answers1

4

To get the position of the carret we have the property selectionStart.

const textarea = document.getElementById('text');
textarea.value = textarea.value.substring(0, textarea.selectionStart) + nikud + textarea.value.substring(textarea.selectionStart, textarea.value.length);
textarea.focus();
IAfanasov
  • 4,775
  • 3
  • 27
  • 42
  • Thank you, but unfortunately, it didn't work - ended up messing the text... The code from here worked: https://stackoverflow.com/questions/11076975/how-to-insert-text-into-the-textarea-at-the-current-cursor-position – Kfir Sadeh May 03 '20 at 00:25
  • 1
    Thanks for the feedback! I corrected the code, should work now. Would be useful for the future readers (: – IAfanasov May 03 '20 at 05:30