How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Select All Text in a Contenteditable Div on Click
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • 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.

You Might Also Like

Check if an element is hidden or visible with jQuery
Web Development

How to Check if an Element Is Hidden or Visible with jQuery

4 Min Read
Install PHP on Ubuntu — terminal with apt install php command and stylized elephant icon
Web Development

How to Install PHP on Ubuntu (22.04 & 24.04): Step-by-Step Guide

9 Min Read
Comment in a .gitignore file with the # character
Web Development

How to Comment in a .gitignore File

4 Min Read
Make the first option selected in a jQuery select
Web Development

How to Make the First Option Selected in a with jQuery

4 Min Read
How7oHow7o

Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

  • Private by design — your files never leave your device
  • Instant — no signup, no install, no waiting
  • Always free — no trials, no watermarks, no paywalls

Tools

  • PDF Editor
  • Image Upscaler
  • JPEG Compressor
  • Password Generator
  • QR Code Generator
  • Video Trimmer
  • See all tools →

Pranks

  • Cracked Screen Prank
  • Fake Blue Screen (BSOD)
  • Fake Disk Format
  • Fake Low Battery
  • Fake Nintendo Switch Update Screen
  • Fake PlayStation Update Screen
  • 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?