I have spent an hour reading a million different posts and can't get a winner. Simply put. I have created an on-screen keyboard. When a user presses a letter button, the letter is inserted at the carat in the input that has focus. This all works fine and I know how to insert all the letters and numbers and spaces but I can't figure out how to backspace at the carat. I know how to take the last character off but that is not effective as I wish it to backspace at the carat.
I will insert the code to show how it is set up... The only part that does not work is the lines in the if ($(this).html() == 'BKSP')
block.
PLEASE and THANKS!
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA and others
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
} else {
myField.value += myValue;
}
}
$("#keyboard").on("pointerdown", function(e){
e.preventDefault();
});
$(".sm-kb-btn").on("pointerdown", function (e) {
if ($(this).html() == 'BKSP') {
var e = new Event("keydown");
e.key = "Backspace";
e.code = "Backspace";
document.getElementById("search-box-input").dispatchEvent(e);
}
else {
insertAtCursor(document.getElementById("search-box-input"), $(this).html());
}
})