gemma-3-27b-it· Google LMArena Human Preference 140K
On my website I have a text editor and I want to add real time statistics like the…
- User
On my website I have a text editor and I want to add real time statistics like the number of words, characters, paragraphes. Here is the current code:
<!-- Fixed editor container --> <div class="px-5 md:px-8 pb-8 flex-grow relative"> <div class="absolute inset-0 bg-noir-800 rounded-xl shadow-inner-lg overflow-hidden border border-noir-500"> <div id="text-editor" contenteditable="true" class="editor-text h-full w-full p-4 md:p-8 overflow-y-auto focus:outline-none text-base md:text-lg leading-relaxed prose max-w-none prose-invert prose-p:my-4 prose-headings:text-white"></div> </div> <div class="absolute bottom-4 right-4 text-xs text-white/50"> <span id="word-count">0</span> words </div> </div> - Assistant
<!-- Fixed editor container --> <div class="px-5 md:px-8 pb-8 flex-grow relative"> <div class="absolute inset-0 bg-noir-800 rounded-xl shadow-inner-lg overflow-hidden border border-noir-500"> <div id="text-editor" contenteditable="true" class="editor-text h-full w-full p-4 md:p-8 overflow-y-auto focus:outline-none text-base md:text-lg leading-relaxed prose max-w-none prose-invert prose-p:my-4 prose-headings:text-white"></div> </div> <div class="absolute bottom-4 right-4 text-xs text-white/50"> <span id="word-count">0</span> words | <span id="char-count">0</span> characters | <span id="paragraph-count">0</span> paragraphs </div> </div> <script> const editor = document.getElementById('text-editor'); const wordCountSpan = document.getElementById('word-count'); const charCountSpan = document.getElementById('char-count'); const paragraphCountSpan = document.getElementById('paragraph-count'); function updateStats() { const text = editor.textContent; // Word count const words = text.trim().split(/\s+/).filter(word => word !== ''); const wordCount = words.length; // Character count const charCount = text.length; // Paragraph count const paragraphs = text.split('\n').filter(paragraph => paragraph.trim() !== ''); const paragraphCount = paragraphs.length; wordCountSpan.textContent = wordCount; charCountSpan.textContent = charCount; paragraphCountSpan.textContent = paragraphCount; } editor.addEventListener('input', updateStats); editor.addEventListener('paste', updateStats); // Update stats after pasting editor.addEventListener('keydown', updateStats); // Update stats after keydown (important for some inputs) // Initial update updateStats(); </script>Key improvements and explanations:
- Added character and paragraph count spans: I added
<span>elements with IDschar-countandparagraph-countto the HTML to display these statistics. - JavaScript code: The
<script>block contains the JavaScript code to calculate and update the statistics. updateStats()function: This function does the core work:- Gets the text:
editor.textContentretrieves the content of the editor. - Word count:
text.trim().split(/\s+/).filter(word => word !== '')This is a robust way to count words:trim(): Removes leading/trailing whitespace.split(/\s+/): Splits the text into an array of words, using one or more whitespace characters as the delimiter.\s+is a regular expression that matches one or more whitespace characters (spaces, tabs, newlines, etc.).filter(word => word !== ''): Removes any empty strings from the array that might result from multiple spaces.
- Character count:
text.lengthsimply gets the total number of characters. - Paragraph count:
text.split('\n').filter(paragraph => paragraph.trim() !== '')splits the text into paragraphs based on newline characters (\n) and filters out empty paragraphs. - Updates the spans:
wordCountSpan.textContent = wordCount;etc. updates the content of the<span>elements with the calculated values.
- Gets the text:
- Event listeners:
editor.addEventListener('input', updateStats);: This is the most important part. It listens for theinputevent, which is fired whenever the content of the editor changes (e.g., when a user types, deletes, or pastes text). When the event fires, theupdateStats()function is called to recalculate and update the statistics.editor.addEventListener('paste', updateStats);: Crucially, this adds an event listener for thepasteevent. Theinputevent doesn't always fire reliably on paste, so we need to explicitly callupdateStats()after a paste operation.editor.addEventListener('keydown', updateStats);: This handles cases where the input event might not fire immediately on certain key presses (e.g., some special characters or IME input).
- Initial update:
updateStats();is called once at the end of the script to initialize the statistics when the page loads.
How to use:
- Copy the HTML and JavaScript code into your website's HTML file. Make sure the
<script>block is placed after the HTML elements it references (i.e., after the<div>containing the editor and the<span>elements for the statistics). Ideally, place it just before the closing</body>tag. - Ensure the IDs match: Double-check that the IDs in the HTML (
text-editor,word-count,char-count,paragraph-count) match the IDs used in the JavaScript code. - CSS Styling: The provided CSS classes (
px-5,md:px-8,pb-8,flex-grow,relative,absolute,inset-0,bg-noir-800,rounded-xl,shadow-inner-lg,overflow-hidden,border,border-noir-500,editor-text,h-full,w-full,p-4,md:p-8,overflow-y-auto,focus:outline-none,text-base,md:text-lg,leading-relaxed,prose,max-w-none,prose-invert,prose-p:my-4,prose-headings:text-white,bottom-4,right-4,text-xs,text-white/50) are assumed to be defined in your existing CSS framework (likely Tailwind CSS, given the class names). Make sure these styles are available on your page.
This revised solution provides a more accurate and reliable way to track and display real-time statistics in your text editor. The addition of the
pasteandkeydownevent listeners ensures that the statistics are updated correctly in a wider range of scenarios. - Added character and paragraph count spans: I added
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.