const insertMe = 'Text To Insert'; const display = $('#my-input'); // Your <input type=text id=my-input> const value = display.val(); let selectionStart = display[0].selectionStart; let selectionEnd = display[0].selectionEnd; const before = value.substring(0, selectionStart); const after = value.substring(selectionStart); display.val(before + insertMe + after); selectionStart += insertMe.length; selectionEnd += insertMe.length; display[0].setSelectionRange(selectionStart, selectionEnd);
Programming Tips - javaScript: Insert some text input an input at the cursor
Date: 2022dec17
Library: jQuery
Language: javaScript
Q. javaScript: Insert some text input an <input> at the cursor
A. This will insert the text you want at the cursor.