Given a DIV element with text content, how can the user programmatically select the entire text within the DIV with a single mouse click? This allows users to easily drag and drop the selected text or copy it directly.
To select the text within a DIV element on a single mouse click, you can utilize the following JavaScript function:
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); } }
To implement this functionality:
With this code, when users click anywhere within the DIV element, the entire text within that DIV will be highlighted and selected, allowing for easy manipulation or copying.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3