1

When I replace the whole content of an editable div by jquery, how can I prevent the cursor from moving to the start after any key press?

$('.editme').on('change keyup', function(e){
    wordFilter($(this).text());
    $(this).text(wordFilter($(this).text()));
}); 

I tried to save and restore the cursor position with selectionStart and selectionEnd, but it doesnt work on the editable div. Is there any simple way in jquery?

UPDATE:

WORKING SOLUTION: (with included caret plugin)

$('.editme').on('change keyup', function(e){
    var caretPos = $(this).caret();
    wordFilter($(this).text());
    $(this).text(wordFilter($(this).text()));
    $(this).caret(caretPos);
});
tms99
  • 25
  • 7
  • Does this answer your question? [How to set the caret (cursor) position in a contenteditable element (div)?](https://stackoverflow.com/questions/6249095/how-to-set-the-caret-cursor-position-in-a-contenteditable-element-div) – Lapskaus Jan 20 '23 at 10:11

0 Answers0