I see that there are a lot of answers already to achieve this using selectionStart
. But unfortunately most of them are related to value
of the field and not the text
.
I have below code but I get Undefined
$(document).ready(function() {
document.getElementById('span_id_1').addEventListener('keyup', e => {
console.log('Caret at: ', e.target.selectionStart)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="span_root" type="text" id="span_id_1" contenteditable="true">Input here</div>