you can create keydown
event
i've seen a function to place the caret the end of the text whenever the the tab key is press. this runs on multiple browsers
check it out : contenteditable, set caret at the end of the text (cross-browser)
$('#test').on( 'keydown', function( e ) {
if( e.which == 9 ) {
e.preventDefault();
$(this).html($(this).html() + " ")
placeCaretAtEnd( document.querySelector('#test') );
}
} );
function placeCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined"
&& typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
div[contenteditable]{
display:inline-block;
font-family:lucida console;
font-size:12px;
line-height:14px;
white-space: pre;
padding:5px;
margin:0;
min-width:200px;
min-height:50px;
height:auto;
background:#fff;
border:1px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div contenteditable="true" id="test">asdfsadf</div>