Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > pl.comp.lang.java > #10875
| From | Andrzej Nowak <rocky@xxx.xx> |
|---|---|
| Newsgroups | pl.comp.lang.java |
| Subject | Re: jak pobrać z niego aktualną pozycję kursora z takiego elementu <div contenteditable="true" id="newpostedit" placeholder="podpowiedż">,</div> |
| Date | 2025-05-24 14:00 +0200 |
| Organization | ICM, Uniwersytet Warszawski |
| Message-ID | <100scdm$10ni5$1@news.icm.edu.pl> (permalink) |
| References | <100pied$japk$1@paganini.bofh.team> |
<div contenteditable="true" id="newpostedit" placeholder="podpowiedż">
</div>
<script>
function getCursorPosition() {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
const preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(document.getElementById('newpostedit'));
preCaretRange.setEnd(range.endContainer, range.endOffset);
const cursorPosition = preCaretRange.toString().length;
return cursorPosition;
}
return 0; // Zwróć 0 jeśli nie ma zaznaczenia lub zakresu
}
// Przykład użycia:
document.getElementById('newpostedit').addEventListener('keyup', () => {
const position = getCursorPosition();
console.log('Pozycja kursora:', position);
});
document.getElementById('newpostedit').addEventListener('click', () => {
const position = getCursorPosition();
console.log('Pozycja kursora po kliknięciu:', position);
});
</script>
Back to pl.comp.lang.java | Previous — Previous in thread | Find similar
jak pobrać z niego aktualną pozycję kursora z takiego elementu <div contenteditable="true" id="newpostedit" placeholder="podpowiedż">,</div> Jakub <jak74@interia.pl> - 2025-05-23 12:27 +0200 Re: jak pobrać z niego aktualną pozycję kursora z takiego elementu <div contenteditable="true" id="newpostedit" placeholder="podpowiedż">,</div> Andrzej Nowak <rocky@xxx.xx> - 2025-05-24 14:00 +0200
csiph-web