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
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Find prime numbers in JavaScript thumbnail
Web Development

How to Find Prime Numbers in JavaScript (1 to 100) — Fast & Simple Methods

5 Min Read
Laravel foreign key constraint linking posts.user_id to users.id in a schema diagram
Web Development

How to Add Foreign Keys in Laravel Migration

6 Min Read
JavaScript check Unicode character — regex, codePointAt, Unicode property escape
Web Development

How to Check if a JavaScript String Contains a Unicode Character

38 Min Read
Get a remote file size from URL in PHP with get_headers
Web Development

How to Get a Remote File’s Size from a URL in PHP

4 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?