I had a similar problem with scrolling to a selection in a text box in FireFox. I could not send a βspaceβ and then the βbackspaceβ character, because that would replace the selection in the text box. Therefore, I found a better way, which was to actually repeat the character immediately after the selection, which would make the selection visible.
Here is the code:
function setSelRange(inputEl, selStart, selEnd) {
if (inputEl.createTextRange) {
var range = inputEl.createTextRange();
range.collapse(true);
range.moveEnd('character', selEnd);
range.moveStart('character', selStart);
range.select();
} else if (inputEl.setSelectionRange) {
inputEl.focus();
inputEl.setSelectionRange(selEnd, selEnd + 1);
var evt = document.createEvent("KeyboardEvent");
if (inputEl.value.length == selEnd) {
evt.initKeyEvent("keypress", true, true, null, false, false, false, false, 0, 32);
} else {
evt.initKeyEvent("keypress", true, true, null, false, false, false, false, 0, inputEl.value.charCodeAt(selEnd));
}
inputEl.dispatchEvent(evt);
inputEl.setSelectionRange(selStart, selEnd);
}
}
Hope this helps anyone looking for this. I spent a lot of time trying to figure it out.
Brian
source
share