How to Select All Text Within a DIV with One Click?
Dec 11, 2024 am 10:22 AMSelect DIV Text Effortlessly with a Single Mouse Click
For quick and convenient text manipulation, it's often desirable to highlight and select the entire contents of a DIV tag. This allows users to easily perform actions such as dragging selected text or copying it with a right-click.
How to Achieve This:
To accomplish this, we can leverage a JavaScript function that utilizes the browser's selection capabilities. Here's how it works:
Code Snippet:
function selectText(containerid) { if (document.selection) { // IE var range = document.body.createTextRange(); range.moveToElementText(document.getElementById(containerid)); range.select(); } else if (window.getSelection) { var range = document.createRange(); range.selectNode(document.getElementById(containerid)); window.getSelection().removeAllRanges(); window.getSelection().addRange(range); } }
Implementation:
In your HTML, include the following code to select text within the DIV element with ID "selectable":
<div>
When the user clicks anywhere within this DIV, the above JavaScript function will execute, highlighting all of its contents. This makes it incredibly easy to manipulate the text without the need for manual selection.
The above is the detailed content of How to Select All Text Within a DIV with One Click?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Adding Box Shadows to WordPress Blocks and Elements

Create a JavaScript Contact Form With the Smart Forms Framework

Demystifying Screen Readers: Accessible Forms & Best Practices

Making Your First Custom Svelte Transition

Create an Inline Text Editor With the contentEditable Attribute

File Upload With Multer in Node.js and Express

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)
