I am trying to set the cursor to the end of next/prev contenteditable tag if the current one is empty, but when I set focus it adds focus to the begining of the text and not end Tried almost all solution on SO but none seemed to work for me. Here the simple code I am trying
HTML CODE
<div id = 'main'>
<div id = 'n1' contenteditable=true> Content one</div>
<div id = 'n2' contenteditable=true> Content 2</div>
<div id = 'n3' contenteditable=true> Content 3</div>
<div id = 'n4' contenteditable=true> Content 4</div>
JS CODE
$('#main div').keydown(function(){
if(!$(this).text().trim()){
$(this).prev().focus();
//setCursorToEnd(this.prev())
}
});
function setCursorToEnd(ele)
{
var range = document.createRange();
var sel = window.getSelection();
range.setStart(ele, 1);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
ele.focus();
}