Snippets Textarea

Bootstrap 5 Textarea with Live Text Highlighting

A textarea that highlights every capitalised word live as you type by mirroring its text in a synced layer behind it, with a switch to reveal that layer.

Bootstrap 5 No jQuery Sep 2019 2.3K views

<section class="wdw-stage">
  <div class="container" style="max-width: 680px">
    <div class="text-center mb-4">
      <span class="wdw-eyebrow">Form control</span>
      <h1 class="wdw-heading h2 mt-1 mb-2">Highlight text inside a textarea</h1>
      <p class="wdw-muted mb-0">Capitalised words are highlighted live as you type.</p>
    </div>

    <div class="wdw-card p-4">
      <label for="hl-input" class="form-label fw-semibold">Your notes</label>
      <div class="hl" id="hl">
        <div class="hl-backdrop" aria-hidden="true"><div class="hl-marks"></div></div>
        <textarea id="hl-input" class="form-control hl-input" rows="7" spellcheck="false">Highlighting inside a textarea is a trick. Browsers cannot style words in a textarea, so a mirror Div sits behind it and wraps each Capitalised word in a Mark. JavaScript keeps the text and scroll position in sync. Edit this text, and any Word that starts with a capital letter lights up. Use the Toggle to see Behind the curtain.</textarea>
      </div>
      <div class="d-flex flex-wrap align-items-center justify-content-between gap-3 mt-3">
        <div class="form-check form-switch m-0">
          <input class="form-check-input" type="checkbox" role="switch" id="hl-toggle">
          <label class="form-check-label" for="hl-toggle">Peek behind the curtain</label>
        </div>
        <span class="small wdw-muted"><span id="hl-count">0</span> highlighted words</span>
      </div>
    </div>
  </div>
</section>

Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.

Free to use in personal and commercial projects under the MIT license.