🚀 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 10:27
jak pobrać z niego aktualną pozycję kursora z takiego elementu <div contenteditable="true" id="newpostedit" placeholder="podpowi...
#154334
Author: Jakub
Date: Fri, 23 May 2025 10:27
6 lines
1143 bytes
Mam taki kod

<div contenteditable="true" 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="pod...
#154325
Author: Andrzej Nowak
Date: Sat, 24 May 2025 12:00
31 lines
1952 bytes

<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>
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