in the example below pls write something inside the textarea and press tab
key
you'll see - it works
I modified its code to use it inside a contenteditable div - but it doesn't work
instead of tab - the textContent is duplicated
please help
$('.btex').on('keydown', function(e){
if (e.keyCode === 9){
var val = this.value;
var start = this.selectionStart;
var end = this.selectionEnd;
this.value = val.substring(0, start) + '\t' + val.substring(end);
this.selectionStart = this.selectionEnd = start + 1;
return false;
}
});
$('.bdiv').on('keydown', function(e){
if (e.keyCode === 9){
var val = this.textContent;
var start = this.selectionStart;
var end = this.selectionEnd;
this.textContent = val.substring(0, start) + '\t' + val.substring(end);
this.selectionStart = this.selectionEnd = start + 1;
return false;
}
});
.btex{display:block; padding:9px; width:100%; min-height:54px; background:lightblue; border:0; outline:none;}
.bdiv{padding:9px; min-height:54px; background:lightblue; overflow-wrap: break-word;
-webkit-user-modify: read-write-plaintext-only; border:0; outline:none;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class='btex'></textarea>
<br>
<div class='bdiv'></div>