JSFiddle - React, Tailwind, and code Playground
HTML
<input value="foobar"></input>
JavaScript
document.querySelectorAll('input').forEach(el => {
el.addEventListener('click', (e) => {
const selectionStart = e.target.selectionStart;
const selectionEnd = e.target.selectionEnd;
console.log('CLICK: ', selectionStart, selectionEnd);
})
})
document.querySelectorAll('input').forEach(el => {
el.addEventListener('select', (e) => {
const selectionStart = e.target.selectionStart;
const selectionEnd = e.target.selectionEnd;
console.log('SELECT: ', selectionStart, selectionEnd);
})
})
function observer() {
console.log(document.querySelector('input').selectionStart, document.querySelector('input').selectionEnd);
setTimeout(observer, 1000);
}
observer();