Dynamic word tracker 2025 indexing working

by Julian

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Text Index Display</title>
  <style>
    textarea {
      width: 100%;
      height: 150px;
      margin-bottom: 10px;
      padding: 10px;
      font-size: 16px;
    }
    button {
      padding: 10px 20px;
      font-size: 16px;
      cursor: pointer;
    }
    pre {
      background-color: #f4f4f4;
      padding: 10px;
      font-size: 14px;
    }
  </style>
</head>
<body>

  <h1>Text Index Display</h1>

  <label for="sourceText">Enter Source Text:</label>
  <textarea id="sourceText" placeholder="Paste the original source text here..." oninput="updateSourceWithIndexes()"></textarea>

  <h2>Source Text with Indexes</h2>
  <pre id="indexedSourceText"></pre>

  <button onclick="addSelectedText()">Paste Selected Text</button>

  <label for="targetText">Target Text Area:</label>
  <textarea id="targetText" readonly placeholder="Selected text will appear here..."></textarea>

  <h2>Word Indexes</h2>
  <pre id="output"></pre>
</body>
</html>

CSS

body {
      font-family: Arial, sans-serif;
      margin: 20px;
    }
    div[contenteditable] {
      border: 1px solid #ccc;
      padding: 10px;
      height: 100px;
      overflow-y: auto;
      margin-bottom: 10px;
    }
    .output {
      margin-top: 10px;
    }

JavaScript

let wordOccurrences = {};

    // Function to update the source text with indexes
    function updateSourceWithIndexes() {
      const sourceText = document.getElementById('sourceText').value.trim();
      const indexedSourceText = document.getElementById('indexedSourceText');

      if (!sourceText) {
        indexedSourceText.textContent = "No source text provided.";
        return;
      }

      // Split the source text into words and track occurrences
      const sourceWords = sourceText.split(/\s+/);
      wordOccurrences = {}; // Reset occurrences tracker

      const indexedWords = sourceWords.map((word, index) => {
        const lowerWord = word.toLowerCase();
        if (!wordOccurrences[lowerWord]) {
          wordOccurrences[lowerWord] = [];
        }
        wordOccurrences[lowerWord].push(index + 1); // Store 1-based index
        return `${word}(${index + 1})`;
      });

      // Display the source text with indexes
      indexedSourceText.textContent = indexedWords.join(' ');
    }

    // Function to add selected text from the source to the target area
    function addSelectedText() {
      const sourceTextArea = document.getElementById('sourceText');
      const targetTextArea = document.getElementById('targetText');
      const outputElement = document.getElementById('output');

      const sourceText = sourceTextArea.value.trim();
      const selectedText = window.getSelection().toString().trim();

      if (!sourceText) {
        outputElement.textContent = "Please enter source text first.";
        return;
      }

      if (!selectedText) {
        outputElement.textContent = "Please select text from the source text.";
        return;
      }

      // Add the selected text to the target text area
      targetTextArea.value += (targetTextArea.value ? ' ' : '') + selectedText;

      // Update indexes for the target text
      updateIndexes(targetTextArea.value, outputElement);
    }

    // Function to update indexes for words in the...