I'm learning jquery and creating a virtual keyboard (to input special characters) for an input textbox. I managed to add the letter on click event to the input at the caret position that the user clicked.
$('#keyboard #e_specchar').click(function(){
var $this = $(this);
var caret = document.getElementById('input').selectionStart;
var character = $this.html();
var textAreaStr = $('#input').val();
$("#input").val(textAreaStr.substring(0, caret) + character + textAreaStr.substring(caret) );
});
However this has some drawbacks too, such as unable to replace the highlighted characters - instead the special characters would appear right next to it; or the caret always jumped to the last position of the input box after a click.
I'm wondering if there is a way to, at least, prevent the caret to jump to the last position after creating a letter from the click event?