🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.java
2 messages
2 total messages Started by Jakub Fri, 23 May 2025 12:27
jak pobrać z niego aktualną pozycję kursora z takiego elementu <div contenteditable="true" id="newpostedit" placeholder="podpowiedż">,</div>
#152146
Author: Jakub
Date: Fri, 23 May 2025 12:27
5 lines
141 bytes
Mam taki kod

<div c id="newpostedit" placeholder="podpowiedż">
</div>

jak pobrać z niego aktualną pozycję cursora?
Re: jak pobrać z niego aktualną pozycję kursora z takiego elementu <div contenteditable="true" id="newpostedit" placeholder="podpowiedż">,</div>
#152147
Author: Andrzej Nowak
Date: Sat, 24 May 2025 14:00
30 lines
969 bytes

<div c 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>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads