How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Select All Text in a Contenteditable Div on Click
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Select All Text in a Contenteditable Div on Click
Web Development

How to Select All Text in a Contenteditable Div on Click

how7o
By how7o
Last updated: May 23, 2026
4 Min Read
Select all text in a contenteditable div on click
SHARE

To select all text inside a contenteditable div when it’s clicked, create a Range covering the div’s contents and apply it as the current Selection. The Range/Selection API is the modern, supported way; document.execCommand('selectAll') still works but is deprecated.

Contents
  • The vanilla JS pattern
  • jQuery variant
  • Inline shortcut (deprecated but works)
  • Select on focus instead of click
  • For form inputs (not contenteditable)
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 in Chrome 124, Firefox 125, Safari 17. Originally published 2024-03-28, rewritten and updated 2026-05-17.

The vanilla JS pattern

const editable = document.getElementById('editable-div');

editable.addEventListener('click', () => {
    const range     = document.createRange();
    range.selectNodeContents(editable);
    const selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
});
  1. document.createRange() — creates a Range object you can position anywhere in the document.
  2. range.selectNodeContents(el) — sets the range to cover everything inside the element (its descendants, not the element itself).
  3. window.getSelection() — the current selection (highlighted text) in the document.
  4. selection.removeAllRanges() — clear any existing selection.
  5. selection.addRange(range) — apply our new range, which highlights everything inside the div.
Select all in contenteditable — Range + Selection API, jQuery variant, deprecated execCommand

jQuery variant

$('#editable-div').on('click', function () {
    const range     = document.createRange();
    range.selectNodeContents(this);
    const selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
});

Inside the handler, this is the raw DOM element, so we pass it directly to selectNodeContents. No need for this[0] or $(this)[0].

Inline shortcut (deprecated but works)

<div
    contenteditable="true"
    id="editable-div"
    onclick="document.execCommand('selectAll', false, null)">
  Click here to edit the text...
</div>

One line of HTML, no JavaScript file. Works today but document.execCommand is officially deprecated — fine for one-off prototypes, not for production.

Select on focus instead of click

editable.addEventListener('focus', () => {
    const range = document.createRange();
    range.selectNodeContents(editable);
    const sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
});

Tabbing into the element auto-selects too. Often the friendlier UX — users get a “ready to type” experience whether they reached the field via tab or click.

For form inputs (not contenteditable)

<input type="text" id="username">

<script>
document.getElementById('username').addEventListener('focus', function () {
    this.select();
});
</script>

<input> and <textarea> have their own .select() method — much simpler than building a Range. Use it for normal form fields; reserve the Range/Selection API for contenteditable.

Frequently asked questions

Why not use document.execCommand('selectAll')?

It works today, but document.execCommand is officially deprecated. MDN flags it as obsolete and browsers may eventually drop support. The Range + Selection API is the supported successor and works in every modern browser. New code should use the Range version.

Will this work inside an <input> or <textarea>?

No — those have their own selection model. For form inputs, use input.select() or input.setSelectionRange(0, input.value.length). The Range/Selection API in this guide is specifically for non-form elements that have contenteditable="true".

How do I select all on focus instead of on click?

Same code, different event: el.addEventListener('focus', selectAll). Note that click fires before focus, and clicking inside the div places the cursor at the click position. Using focus instead means tabbing into the element also auto-selects, which is often the friendlier UX.

Can I select a specific child node or a sub-range?

Yes — range.selectNode(child) selects a single child element (including the wrapper), and range.setStart(node, offset) + range.setEnd(...) pins both ends of the selection. selectNodeContents is the “everything inside this element” shortcut.

Related guides

  • How to Check if a Bootstrap Modal Is Open with jQuery
  • How to Disable or Enable an Input with JavaScript or jQuery
  • How to Duplicate a DIV into Another DIV with jQuery

References

MDN Range: developer.mozilla.org/en-US/docs/Web/API/Range. MDN Selection: developer.mozilla.org/en-US/docs/Web/API/Selection. MDN contenteditable: developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable.

TAGGED:domJavaScriptjQuery

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Scroll to an element on a web page with jQuery How to Scroll to an Element Using jQuery
Next Article Select the last child element with jQuery How to Select the Last Child Element in jQuery
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

How to use localStorage in JavaScript shown in a browser storage panel
Web Development

How to Use localStorage in JavaScript (With Real Examples + Troubleshooting)

8 Min Read
Run JS code on URL hash change with the hashchange event
Web Development

How to Run Code on URL Hash Change in JavaScript

4 Min Read
Install Composer on Ubuntu — terminal with composer-setup.php and PHP elephant icon
Web Development

How to Install Composer on Ubuntu: Step-by-Step Guide

8 Min Read
Laravel DataTables custom column search — filterColumn callback handles the search SQL
Web Development

How to Search Custom or Composite Columns in Laravel DataTables

8 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?