Select all DIV text with single mouse click
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); }}
<div id="selectable" onclick="selectText('selectable')">http://example.com/page.htm</div>
Select All Text in Div Tag (That's Uses Classes) When It's Clicked
You could do something like this:
function getData(element){ if (document.body.createTextRange) { var range = document.body.createTextRange(); range.moveToElementText(element); range.select(); } else if (window.getSelection) { var selection = window.getSelection(); var range = document.createRange(); range.selectNodeContents(element); selection.removeAllRanges(); selection.addRange(range); }
}
<html> <head>
</head> <body> <td> <div class="code" onclick="getData(this)">int variable_name;</div> <div class="code" onclick="getData(this)">int variable_name = value;</div> <div class="code" onclick="getData(this)">float variable_name;</div> <div class="code" onclick="getData(this)">float variable_name = value;</div> <!-- etc... --> </td> </body></html>
How to select div text on button click?
Try the below @Rajagopal Subramanian
function selectText(containerid) { if (document.selection) { 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().addRange(range); } }
<div id="divid">Hello This div content have to be select.</div><button onclick="selectText('divid')">Select</button>
Auto select text when clicked
Can the text be inside of an input like this or do you need the text inside of a div?
You can replace "Click Here To Copy This Text to Clipboard" with anything you want like such as "dummytext with two paragraphs"
$(function() {
$('.copy-text-to-clipboard input').click(function() {
$(this).focus();
$(this).select();
document.execCommand('copy');
$(".copied").text("Copied the text to clipboard").show().fadeOut(3000);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class='copied text'></div>
<div class="copy-text-to-clipboard">
<input readonly type="text" value="Click Here To Copy This Text to Clipboard" readonly>
</div>
</div>
How to select entire word when single click
Hope provided jsfiddle helps.
http://jsfiddle.net/MattCMC/Vap7C/1875/
You can define this as a class click function:
$(".clickable").click(function(e) {
s = window.getSelection();
var range = s.getRangeAt(0);
var node = s.anchorNode;
while (range.toString().indexOf(' ') != 0) {
range.setStart(node, (range.startOffset - 1));
}
range.setStart(node, range.startOffset + 1);
do {
range.setEnd(node, range.endOffset + 1);
}
while (range.toString().indexOf(' ') == -1 && range.toString().trim() != '');
var str = range.toString().trim();
//alert(str);
});
Related Topics
What Is the Scope of Variables in JavaScript
Difference Between "Let" and "Var"
What Is the Purpose of the Var Keyword and When Should I Use It (Or Omit It)
Execute a Command Line Binary With Node.Js
Upload Progress Indicators For Fetch
Cross-Origin Data in Html5 Canvas
Benefits of Loading Js At the Bottom as Opposed to the Top of the Document
How to Detect "Shift+Enter" and Generate a New Line in Textarea
What's Standard Behavior When ≪Button≫ Element Is Clicked? Will It Submit the Form
Does Schema.Org Markup Work If Markup Is Dynamically Built With JavaScript
Why Is ≪Marquee≫ Deprecated and What Is the Best Alternative
Can Jquery Get All CSS Styles Associated With an Element
Restart Animation in Css3: Any Better Way Than Removing the Element
Add Active Navigation Class Based on Url
How to Get the Actual Rendered Font When It's Not Defined in Css
How to Change Style of Iframe Content Cross-Domain
Node.Js App Can't Run on Port 80 Even Though There's No Other Process Blocking the Port