JSFiddle - React, Tailwind, and code Playground

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>Indexed Text with Target Text Area</title>
  <style>
    .indexed-word {
      display: inline-block;
      margin: 0 2px;
      position: relative;
    }
    .indexed-word sup {
      font-size: 0.6em;
      vertical-align: top;
    }
    textarea {
      width: 90%;
      height: 100px;
      margin: 10px 0;
      padding: 5px;
      font-size: 1rem;
    }
    #output {
      border: 1px solid #ccc;
      padding: 10px;
      width: 90%;
      min-height: 150px;
    }
  </style>
</head>
<body>
  <h1>Indexed Text with Target Input</h1>

  <!-- Target Text Area -->
  <textarea id="input-text" placeholder="Paste or type text here to index..."></textarea>

  <!-- Output Display -->
  <div id="output" contenteditable="true">
    <!-- Indexed text will appear here -->
  </div>

  <!-- Download Button -->
  <button id="download">Download JSON</button>

  <script>
    const inputTextarea = document.getElementById('input-text');
    const outputDiv = document.getElementById('output');
    const downloadButton = document.getElementById('download');

    // Function to index words and display them
    function indexWords(inputText) {
      const words = inputText.split(/\s+/);
      const indexedWords = words.map((word, index) => {
        return `<span class="indexed-word">${word}<sup>${index}</sup></span>`;
      });
      return indexedWords.join(' ');
    }

    // Update the output when text is entered in the textarea
    inputTextarea.addEventListener('input', () => {
      const text = inputTextarea.value.trim();
      outputDiv.innerHTML = indexWords(text);
    });

    // Prepare JSON data
    function generateJSON(inputText) {
      const words = inputText.split(/\s+/);
      const jsonData = words.map((word, index) => ({ index, word }));
      return jsonData;
    }

    // Download JSON file
   ...